finished init command - it now initialises the stuff
This commit is contained in:
@@ -1,14 +1,34 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"github.com/edindazdarevic/confighub/chub/globals"
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"bytes"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init(machine *Machine) {
|
func Init(machine *Machine) bool {
|
||||||
req, err := http.NewRequest("POST", url, nil)
|
|
||||||
|
machineByteContent, err := json.Marshal(machine)
|
||||||
if err != nil {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,39 @@
|
|||||||
package globals
|
package globals
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ConfigurationFolderName = ".chub"
|
ConfigurationFolderName = ".chub"
|
||||||
ConfigurationFilePermissions = 600
|
ConfigurationFilePermissions = 600
|
||||||
ConfigurationFileName = "config"
|
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
|
||||||
|
}
|
||||||
40
chub/init.go
40
chub/init.go
@@ -1,21 +1,30 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"chub/client"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/edindazdarevic/confighub/chub/client"
|
||||||
|
"github.com/edindazdarevic/confighub/chub/globals"
|
||||||
homedir "github.com/mitchellh/go-homedir"
|
homedir "github.com/mitchellh/go-homedir"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initCommand(guid string) {
|
func initCommand(guid string) {
|
||||||
confguration := config{MachineGuid: guid}
|
configuration := config{MachineGuid: guid}
|
||||||
saveGuidToHomedir(configuration)
|
saveGuidToHomedir(configuration)
|
||||||
|
|
||||||
machineToInitialize := client.Machine{os.Hostname(), runtime.GOOS, runtime.GOARCH, guid}
|
hostname, err := os.Hostname()
|
||||||
client.init(&machineToInitialize)
|
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) {
|
func saveGuidToHomedir(configuration config) {
|
||||||
|
|
||||||
directory := homedir.Expand() + filepath.Separator + ConfigurationFolderName
|
directoryName, err := homedir.Dir()
|
||||||
if err := os.Mkdir(directory, ConfigurationFilePermissions); err != nil {
|
globals.PanicWithStyle("I cannot find your home directory. Are you homeless ? ", err)
|
||||||
panic(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)
|
configByteContent, err := json.Marshal(configuration)
|
||||||
if err != nil {
|
globals.PanicWithStyle("Something is terribly wrong. I am sorry. I am so so sorry. :~(", err)
|
||||||
panic("Something is terribly wrong. I am sorry. I am so so sorry. :~(")
|
|
||||||
}
|
|
||||||
|
|
||||||
err := ioutil.WriteFile(directory+filepath.Separator+ConfigurationFileName, configByteContent, ConfigurationFilePermissions)
|
err = ioutil.WriteFile(directory+globals.RuneToAscii(filepath.Separator)+globals.ConfigurationFileName, configByteContent, globals.ConfigurationFilePermissions)
|
||||||
if err != nil {
|
globals.PanicWithStyle("I cannot write to config file in user directory. How can this be ?", err)
|
||||||
panic("I cannot write to config file in user directory. How can this be ?")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user