From bc0166731cc9bdb15352ba4d67d879e9e0dedc9b Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Thu, 28 May 2015 18:57:52 +0200 Subject: [PATCH 1/3] created init client method and init command --- .DS_Store | Bin 0 -> 6148 bytes chub/.DS_Store | Bin 0 -> 6148 bytes chub/client/init.go | 14 ++++++++++++++ chub/client/machine.go | 8 ++++++++ chub/globals/.DS_Store | Bin 0 -> 6148 bytes chub/globals/globals.go | 8 ++++++++ chub/init.go | 36 ++++++++++++++++++++++++++++++++++-- 7 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 .DS_Store create mode 100644 chub/.DS_Store create mode 100644 chub/client/init.go create mode 100644 chub/client/machine.go create mode 100644 chub/globals/.DS_Store create mode 100644 chub/globals/globals.go diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..427cbce204159626b5bd863ac98b802b8a2dc780 GIT binary patch literal 6148 zcmeHKUrQT75T8xb_M$IWYC-Vj9xa3twIx2DF$#SUf+>nrN$Ev%539>~D|V9RLu`VXy*V1Av7}SgK-ii%>u5 z4JnzH3ZkGfUPZ~VA0s{KflO&qjp1&u< zylXFPCUNG;I2>FFJ=~XZkmSu|7zgQLM~}BJgRIxJpSB0#35K@4INXz|^kpQoQ>#i} z*IpcrR@c{R&T3<2TysW^M!n{|SZj>O_IJx#diHW-=b%4+|Ka1O&tJZNJ3CiP27y1e zk|zc);RhO3*o7_FfEMf^K8JS@%ux6fy_P3RRA3o$2xo~FyE9(Ol?!Ho8DIwPj{$SE zS@ZWd1&_=OFa!T619Uz(sD!S;LZdo5uuv%gB0VFOpsk+%hz>O%x&{l4*n%Q-D54G( zCdCjYa}YWl`(5K-Xw=~#)XX@JnOT?&MVQ1xILaM_tC34)fEl>XKv`F9y8pNC&i}6m zag7;Z2L3AsM5WbfHE~JiZoRQMx@#TOx2PoKS7@AuprNi}^rfqK5mgDcJ7gfb1`CbY Qg2E30MFSViz#nDc0z@Qy{r~^~ literal 0 HcmV?d00001 diff --git a/chub/.DS_Store b/chub/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a56625feef5be56b769f9907a2f8ccc55b0ad793 GIT binary patch literal 6148 zcmeHK&ubGw6n>M&WYb<&v{3M}M};71tHk3PtKdNhMq0#5+-(*!u!%5+418d12ie)rIKb*4;Bqyk<{k|IhdU*UdV z;a2H6xG9Ac0JCMbc*tycX*N?Mudu^$HZZ@VpWz^uS>9;;5T&*9g-X?_t#9z-UR;0L z>8ImP9^|8}6%3!Kn(RfPiQ7TmpU02VG(L%ga9>8_u6y}Tn&g2@qTxd7;ekwg>A01S zl3sS$(c>M+Uf%Dzo9$ur5~=MViS}hCLmA8b*s1Z;b+;yyom;zgZ>PCEt$UMZvr+eM z>^7%U_p{?%xpwpRgU5sEt2b}oz5np>)0eMm;w13LQFh7T9KIo_LhJ;d({;$1E{jOK zJg*m)r%Ep%HfNm0<^@`0&);LwDqs~jmkOAz$JscSv$vDB3RngHqyX;^9-PrPSZGvR z2MTos0G83L46*w32YTE9^bHmo(E<}X6{u5%xnc-&a}YWm{l4KB8g)7e^=6D?-Ym=w zMVO0+@D!bduhEuP0jt2W0&BYJ^7()F_xJzhBwMozSOxwo1w?tTv)96u%-MQsa(vc$ t@Mmx~<`o)GQV^)47+gMzx8Taq?r{M04Hg>F0<#|iN(Nh41^%i6zW_+nnbH6N literal 0 HcmV?d00001 diff --git a/chub/client/init.go b/chub/client/init.go new file mode 100644 index 0000000..3f5c9db --- /dev/null +++ b/chub/client/init.go @@ -0,0 +1,14 @@ +package client + +import ( + "io/ioutil" + "net/http" +) + +func Init(machine *Machine) { + req, err := http.NewRequest("POST", url, nil) + if err != nil { + return nil, err + } + +} 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/.DS_Store b/chub/globals/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..53fa1c418b41eacac33b6cf9ffca09d8f7b3fe85 GIT binary patch literal 6148 zcmeHKF-`+P474Fd1WiiHEvbXpm^oSh8o= z>$%NMagNQ*x35pvW*akG!-@9Bur!|27j{w^2g31;2OI6huI$;SMfLXya?emdf-!vM z-;UGcFir!``QvWjmmgtR$)tc3kOERb3P^$9D!|?gTc0N?N&zV#1wIwv_o2awy>L#9 zPX|Jb0KgT}VOYm30c=bFd*Pgj49t@XOsdz2;YmlnRb4Nf6O(QpH{&^Vv)6>;aXa!Y z%FTPCq7;w<#|oV0cEkR^hyO7DACt6`0#e{#Dd4N!-ENCls@}SIIriEHzlXEt9ZthK oC Date: Thu, 28 May 2015 18:58:26 +0200 Subject: [PATCH 2/3] added ds store to ignore --- .gitignore | 1 + chub/globals/.DS_Store | Bin 6148 -> 0 bytes 2 files changed, 1 insertion(+) delete mode 100644 chub/globals/.DS_Store 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/globals/.DS_Store b/chub/globals/.DS_Store deleted file mode 100644 index 53fa1c418b41eacac33b6cf9ffca09d8f7b3fe85..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKF-`+P474Fd1WiiHEvbXpm^oSh8o= z>$%NMagNQ*x35pvW*akG!-@9Bur!|27j{w^2g31;2OI6huI$;SMfLXya?emdf-!vM z-;UGcFir!``QvWjmmgtR$)tc3kOERb3P^$9D!|?gTc0N?N&zV#1wIwv_o2awy>L#9 zPX|Jb0KgT}VOYm30c=bFd*Pgj49t@XOsdz2;YmlnRb4Nf6O(QpH{&^Vv)6>;aXa!Y z%FTPCq7;w<#|oV0cEkR^hyO7DACt6`0#e{#Dd4N!-ENCls@}SIIriEHzlXEt9ZthK oC Date: Fri, 29 May 2015 21:10:00 +0200 Subject: [PATCH 3/3] finished init command - it now initialises the stuff --- chub/client/init.go | 28 ++++++++++++++++++++++++---- chub/globals/globals.go | 33 ++++++++++++++++++++++++++++++++- chub/init.go | 40 ++++++++++++++++++++++++++-------------- 3 files changed, 82 insertions(+), 19 deletions(-) diff --git a/chub/client/init.go b/chub/client/init.go index 3f5c9db..130cc7c 100644 --- a/chub/client/init.go +++ b/chub/client/init.go @@ -1,14 +1,34 @@ package client import ( - "io/ioutil" + "github.com/edindazdarevic/confighub/chub/globals" + "encoding/json" "net/http" + "bytes" ) -func Init(machine *Machine) { - req, err := http.NewRequest("POST", url, nil) +func Init(machine *Machine) bool { + + machineByteContent, err := json.Marshal(machine) if err != nil { - return nil, err + 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/globals/globals.go b/chub/globals/globals.go index ca38a66..0b959b3 100644 --- a/chub/globals/globals.go +++ b/chub/globals/globals.go @@ -1,8 +1,39 @@ package globals +import ( + "fmt" + "strconv" + "os" +) + + const ( ConfigurationFolderName = ".chub" ConfigurationFilePermissions = 600 ConfigurationFileName = "config" - UrlOfTheService = "confighub.meteor.com" + 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 5125e10..d326f6f 100644 --- a/chub/init.go +++ b/chub/init.go @@ -1,21 +1,30 @@ package main import ( - "chub/client" "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) { - confguration := config{MachineGuid: guid} + configuration := config{MachineGuid: guid} saveGuidToHomedir(configuration) - machineToInitialize := client.Machine{os.Hostname(), runtime.GOOS, runtime.GOARCH, guid} - client.init(&machineToInitialize) + 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) + } } @@ -25,19 +34,22 @@ type config struct { func saveGuidToHomedir(configuration config) { - directory := homedir.Expand() + filepath.Separator + ConfigurationFolderName - if err := os.Mkdir(directory, ConfigurationFilePermissions); err != nil { - panic(err) + 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) - if err != nil { - panic("Something is terribly wrong. I am sorry. I am so so sorry. :~(") - } + globals.PanicWithStyle("Something is terribly wrong. I am sorry. I am so so sorry. :~(", err) - err := ioutil.WriteFile(directory+filepath.Separator+ConfigurationFileName, configByteContent, ConfigurationFilePermissions) - if err != nil { - panic("I cannot write to config file in user directory. How can this be ?") - } + 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) }