Merge branch 'master' of https://github.com/edazdarevic/confighub
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
web/.meteor/local
|
web/.meteor/local
|
||||||
chub/chub
|
chub/chub
|
||||||
|
.DS_Store
|
||||||
|
|||||||
BIN
chub/.DS_Store
vendored
Normal file
BIN
chub/.DS_Store
vendored
Normal file
Binary file not shown.
34
chub/client/init.go
Normal file
34
chub/client/init.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
8
chub/client/machine.go
Normal file
8
chub/client/machine.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
type Machine struct {
|
||||||
|
Hostname string
|
||||||
|
Platform string
|
||||||
|
Architecture string
|
||||||
|
MachineGuid string
|
||||||
|
}
|
||||||
39
chub/globals/globals.go
Normal file
39
chub/globals/globals.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
46
chub/init.go
46
chub/init.go
@@ -1,11 +1,55 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
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"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initCommand(guid string) {
|
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)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user