finished init command - it now initialises the stuff
This commit is contained in:
40
chub/init.go
40
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)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user