finished init command - it now initialises the stuff

This commit is contained in:
Senad Uka
2015-05-29 21:10:00 +02:00
parent 67d5350589
commit aa0b472cb1
3 changed files with 82 additions and 19 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)
}