diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..427cbce Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index c94ed1b..04e88c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ web/.meteor/local chub/chub +.DS_Store diff --git a/chub/.DS_Store b/chub/.DS_Store new file mode 100644 index 0000000..a56625f Binary files /dev/null and b/chub/.DS_Store differ diff --git a/chub/client/init.go b/chub/client/init.go new file mode 100644 index 0000000..130cc7c --- /dev/null +++ b/chub/client/init.go @@ -0,0 +1,34 @@ +package client + +import ( + "github.com/edindazdarevic/confighub/chub/globals" + "encoding/json" + "net/http" + "bytes" +) + +func Init(machine *Machine) bool { + + machineByteContent, err := json.Marshal(machine) + if err != nil { + globals.PanicWithStyle("I am so ashamed. But something happened that just should not happen. :~(",err) + } + + resourceUrl := globals.UrlOfTheService + "machine" + req, err := http.NewRequest("POST", resourceUrl, bytes.NewBuffer(machineByteContent)) + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + response, err := client.Do(req) + if err != nil { + globals.PanicWithStyle("Ah. I cannot get to the server to init your machine. Are you sure you have connection to internet ?",err) + } + + defer response.Body.Close() + + if response.StatusCode != http.StatusOK { + globals.PanicWithStyle("Something is wrong on our end. Ask us about it on twitter, facebook, email, phone - you can even fax as. Smoke signals will not work. ", err) + } + + return true +} diff --git a/chub/client/machine.go b/chub/client/machine.go new file mode 100644 index 0000000..7b6c05e --- /dev/null +++ b/chub/client/machine.go @@ -0,0 +1,8 @@ +package client + +type Machine struct { + Hostname string + Platform string + Architecture string + MachineGuid string +} diff --git a/chub/globals/globals.go b/chub/globals/globals.go new file mode 100644 index 0000000..0b959b3 --- /dev/null +++ b/chub/globals/globals.go @@ -0,0 +1,39 @@ +package globals + +import ( + "fmt" + "strconv" + "os" +) + + +const ( + ConfigurationFolderName = ".chub" + ConfigurationFilePermissions = 600 + ConfigurationFileName = "config" + UrlOfTheService = "http://localhost:3000/api/" + MimeTypeOfService = "application/json" +) + + +func PanicWithStyle(message string, err error) { + if err != nil { + panic(fmt.Sprintf("%v:\n%v", message, err)) + } +} + +func RuneToAscii(r rune) string { + if r < 128 { + return string(r) + } else { + return "\\u" + strconv.FormatInt(int64(r), 16) + } +} + +func FileExists(path string) (bool) { + _, err := os.Stat(path) + if err == nil { return true } + if os.IsNotExist(err) { return false } + PanicWithStyle("This should not be happening. Call 911 !" , err) + return false +} \ No newline at end of file diff --git a/chub/init.go b/chub/init.go index 694c078..d326f6f 100644 --- a/chub/init.go +++ b/chub/init.go @@ -1,11 +1,55 @@ package main import ( + "encoding/json" + "github.com/edindazdarevic/confighub/chub/client" + "github.com/edindazdarevic/confighub/chub/globals" + homedir "github.com/mitchellh/go-homedir" + "io/ioutil" + "os" + "path/filepath" + "runtime" "fmt" ) func initCommand(guid string) { + configuration := config{MachineGuid: guid} + saveGuidToHomedir(configuration) - fmt.Printf("initializing %v\n", guid) + hostname, err := os.Hostname() + globals.PanicWithStyle("Something is terribly wrong. I am sorry. I am so so sorry. :~(", err) + + machineToInitialize := client.Machine{hostname, runtime.GOOS, runtime.GOARCH, guid} + finishedOk := client.Init(&machineToInitialize) + + if (finishedOk) { + fmt.Printf("Successfuly initialized the machine with id %v\n", guid) + } + +} + +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) }