56 lines
1.8 KiB
Go
56 lines
1.8 KiB
Go
package main
|
|
|
|
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"
|
|
)
|
|
|
|
func initCommand(guid string) {
|
|
configuration := config{MachineGuid: guid}
|
|
saveGuidToHomedir(configuration)
|
|
|
|
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)
|
|
|
|
}
|