From f2bfd685e57a198e946b28173227c9d691c182ea Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sat, 30 May 2015 11:02:31 +0200 Subject: [PATCH] checking for config file --- chub/add.go | 5 ++++ chub/config.go | 55 +++++++++++++++++++++++++++++++++++++++++ chub/globals/globals.go | 3 ++- chub/init.go | 30 ---------------------- chub/main.go | 39 ++++++++++++++++++++++++++--- 5 files changed, 98 insertions(+), 34 deletions(-) create mode 100644 chub/add.go create mode 100644 chub/config.go diff --git a/chub/add.go b/chub/add.go new file mode 100644 index 0000000..194e7d9 --- /dev/null +++ b/chub/add.go @@ -0,0 +1,5 @@ +package main + +func addCommand(pathOfFile string, typeOfFile string) { + return +} diff --git a/chub/config.go b/chub/config.go new file mode 100644 index 0000000..031cf22 --- /dev/null +++ b/chub/config.go @@ -0,0 +1,55 @@ +package main + +import ( + "encoding/json" + "github.com/edindazdarevic/confighub/chub/globals" + homedir "github.com/mitchellh/go-homedir" + "io/ioutil" + "os" + "path/filepath" +) + +type config struct { + MachineGuid string +} + +func configurationDirectoryName() string { + directoryName, err := homedir.Dir() + globals.PanicWithStyle("I cannot find your home directory. Are you homeless ? ", err) + + expandedDirectoryName, err := homedir.Expand(directoryName) + globals.PanicWithStyle("Cannot know your full path to home directory. Is it a secret ? ", err) + + return expandedDirectoryName + globals.RuneToAscii(filepath.Separator) + globals.ConfigurationDirectoryName +} + +func configurationFilePath() string { + + return configurationDirectoryName() + globals.RuneToAscii(filepath.Separator) + globals.ConfigurationFileName + +} + +func saveGuidToHomedir(configuration config) { + directory := configurationDirectoryName() + if !globals.FileExists(directory) { + err := os.Mkdir(directory, globals.ConfigurationFilePermissions) + globals.PanicWithStyle("I simply cannot make the directory. Am I alowed to ?", err) + } + + configByteContent, err := json.Marshal(configuration) + globals.PanicWithStyle("Something is terribly wrong. I am sorry. I am so so sorry. :~(", err) + + err = ioutil.WriteFile(configurationFilePath(), configByteContent, globals.ConfigurationFilePermissions) + globals.PanicWithStyle("I cannot write to config file in user directory. How can this be ?", err) +} + +func loadGuidFromHomedir() config { + result := config{} + + file, err := ioutil.ReadFile(configurationFilePath()) + globals.PanicWithStyle("I cannot read config file in user directory. How can this be ?", err) + + json.Unmarshal(file, &result) + return result + +} diff --git a/chub/globals/globals.go b/chub/globals/globals.go index 0b959b3..fb6f446 100644 --- a/chub/globals/globals.go +++ b/chub/globals/globals.go @@ -8,11 +8,12 @@ import ( const ( - ConfigurationFolderName = ".chub" + ConfigurationDirectoryName = ".chub" ConfigurationFilePermissions = 600 ConfigurationFileName = "config" UrlOfTheService = "http://localhost:3000/api/" MimeTypeOfService = "application/json" + WebSite = "http://confighub.saburly.com" ) diff --git a/chub/init.go b/chub/init.go index 71bb359..2731b91 100644 --- a/chub/init.go +++ b/chub/init.go @@ -1,14 +1,10 @@ package main import ( - "encoding/json" "fmt" "github.com/edindazdarevic/confighub/chub/client" "github.com/edindazdarevic/confighub/chub/globals" - homedir "github.com/mitchellh/go-homedir" - "io/ioutil" "os" - "path/filepath" "runtime" ) @@ -27,29 +23,3 @@ func initCommand(guid string) { } } - -type config struct { - MachineGuid string -} - -func saveGuidToHomedir(configuration config) { - - directoryName, err := homedir.Dir() - globals.PanicWithStyle("I cannot find your home directory. Are you homeless ? ", err) - - expandedDirectoryName, err := homedir.Expand(directoryName) - globals.PanicWithStyle("Cannot know your full path to home directory. Is it a secret ? ", err) - - directory := expandedDirectoryName + globals.RuneToAscii(filepath.Separator) + globals.ConfigurationFolderName - if !globals.FileExists(directory) { - err = os.Mkdir(directory, globals.ConfigurationFilePermissions) - globals.PanicWithStyle("I simply cannot make the directory. Am I alowed to ?", err) - } - - configByteContent, err := json.Marshal(configuration) - globals.PanicWithStyle("Something is terribly wrong. I am sorry. I am so so sorry. :~(", err) - - err = ioutil.WriteFile(directory+globals.RuneToAscii(filepath.Separator)+globals.ConfigurationFileName, configByteContent, globals.ConfigurationFilePermissions) - globals.PanicWithStyle("I cannot write to config file in user directory. How can this be ?", err) - -} diff --git a/chub/main.go b/chub/main.go index 461205b..780fdf7 100644 --- a/chub/main.go +++ b/chub/main.go @@ -2,21 +2,41 @@ package main import ( "fmt" + "github.com/edindazdarevic/confighub/chub/globals" "os" ) func main() { - if len(os.Args[1:]) < 2 { + if len(os.Args[1:]) < 1 { usage() - return } command := os.Args[1] + if command != "init" && !globals.FileExists(configurationFilePath()) { + initializeFirst() + } + switch { + case command == "init": + if len(os.Args[1:]) < 2 { + usage() + } machineId := os.Args[2] initCommand(machineId) + + case command == "add": + if len(os.Args[1:]) < 2 { + usage() + } + pathToConfFile := os.Args[2] + typeOfConfFile := "unknown" + if len(os.Args[1:]) >= 3 { + typeOfConfFile = os.Args[3] + } + addCommand(pathToConfFile, typeOfConfFile) + } } @@ -25,5 +45,18 @@ func usage() { fmt.Println("Configuration Hub v0.1") fmt.Println("") fmt.Println("Usage:") - fmt.Println("\tchub init - connects your server to the config hub machine id ") + fmt.Println(" chub init - connects your server to the config hub machine id ") + fmt.Println(" chub add /path/to/file.conf - starts tracking configuration file on that path") + fmt.Println(" This thingy can be one of the following:\n") + fmt.Println(" nginx\n elasticsearch\n") + fmt.Println(" Or just leave it out and I will try to autodetect it (and sometimes fail)\n") + os.Exit(1) +} + +func initializeFirst() { + fmt.Println("Configuration Hub v0.1") + fmt.Println("") + fmt.Println("There is no configuration file present. You need to run chub init first!") + fmt.Printf("Checkout %v for details!\n", globals.WebSite) + os.Exit(1) }