Skip to content
Snippets Groups Projects
Commit 368b3a6f authored by Olexandr Shalakhin's avatar Olexandr Shalakhin
Browse files

Issue #7 - implemented basic support for .cloudignore

parent 6a68c6af
Branches
Tags
No related merge requests found
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
......@@ -11,6 +12,8 @@ import (
"os"
"os/user"
"path"
"path/filepath"
"regexp"
)
const (
......@@ -22,6 +25,30 @@ const (
CLOUDIGNORE = ".cloudignore"
)
var (
ignorelist = GetIgnoreList()
)
// GetIgnoreList returns pattern to ignore paths based on .cloudignore
func GetIgnoreList() []string {
f, err := os.Open(CLOUDIGNORE)
if err != nil {
return []string{"a^"}
}
str := []string{}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
text := scanner.Text()
if text[0:2] != "//" {
str = append(str, text)
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
return str
}
// GetCore returns parsed .cloudcore struct
func GetCore() (storage.Core, error) {
core := storage.Core{}
......@@ -96,8 +123,8 @@ func IsExists(filename string) bool {
return true
}
// Create config
func Create(filename string, v interface{}) error {
// CreateConfig with filename and defined structure
func CreateConfig(filename string, v interface{}) error {
template, err := json.MarshalIndent(v, "", " ")
if err != nil {
panic(err)
......@@ -127,7 +154,7 @@ func initConfigs() {
},
},
}
if err := Create(cloudcorepath, core); err != nil {
if err := CreateConfig(cloudcorepath, core); err != nil {
panic(err)
}
fmt.Println("Initializing file:\t", cloudcorepath)
......@@ -142,12 +169,18 @@ func initConfigs() {
},
},
}
if err := Create(CLOUD, cloud); err != nil {
if err := CreateConfig(CLOUD, cloud); err != nil {
panic(err)
}
fmt.Println("Initializing file:\t", CLOUD)
}
// TODO cloudignore
// cloudignore
if ok := IsExists(CLOUDIGNORE); !ok {
if err = ioutil.WriteFile(CLOUDIGNORE, []byte("// Put here what to ignore. Syntax like .gitignore\n.cloud\n.cloudignore\n"), 0644); err != nil {
panic(err)
}
fmt.Println("Initializing file:\t", CLOUDIGNORE)
}
}
// Sync folder with defined container name string (default is the first container in the list in local .cloud)
......@@ -179,7 +212,7 @@ func Sync(name string) {
if err != nil {
panic(err)
}
fmt.Println("Container found:", c.Name)
fmt.Println("Container found:\t", c.Name)
if err != nil {
panic(err)
}
......@@ -194,6 +227,40 @@ func Sync(name string) {
panic(err)
}
fmt.Println("Authenticated")
return
// walk files upload file to the cloud
dir, err := os.Getwd()
if err != nil {
panic(err)
}
if err = filepath.Walk(dir, func(filename string, info os.FileInfo, err error) error {
// upload only files, not directories
if !info.IsDir() {
fp, err := filepath.Rel(dir, filename)
if err != nil {
panic(err)
}
if !IsIgnored(fp) {
fmt.Println(fp)
}
}
return nil
}); err != nil {
panic(err)
}
}
// IsIgnored path or not. Data is taken from .cloudignore
func IsIgnored(filename string) bool {
// TODO I am sure it can be done in more elegant and efficient way
for _, v := range ignorelist {
re, err := regexp.Compile("^" + v)
if err != nil {
panic(err)
}
// if match found file must be ignored
if re.MatchString(filename) {
return true
}
}
return false
}
......@@ -16,6 +16,7 @@ const (
COMMANDS = "COMMANDS\n" +
"\tinit\tinitialize .cloudcore and .cloud files\n" +
"\tsync\tsynchronize folder with the cloud\n" +
"\tignore\tignore particular file with .cloudignore\n" +
"\tclear\tclear container\n" +
"\thelp\tshow this message\n\n"
// CONTRIBUTORS shows package author & contributors
......@@ -44,6 +45,8 @@ func main() {
container = args[1]
}
Sync(container)
case args[0] == "ignore":
fmt.Println("Nothing here yet...")
case args[0] == "clear":
fmt.Println("Nothing here yet...")
case args[0] == "help":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment