44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"chub/client"
|
|
"encoding/json"
|
|
homedir "github.com/mitchellh/go-homedir"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
)
|
|
|
|
func initCommand(guid string) {
|
|
confguration := config{MachineGuid: guid}
|
|
saveGuidToHomedir(configuration)
|
|
|
|
machineToInitialize := client.Machine{os.Hostname(), runtime.GOOS, runtime.GOARCH, guid}
|
|
client.init(&machineToInitialize)
|
|
|
|
}
|
|
|
|
type config struct {
|
|
MachineGuid string
|
|
}
|
|
|
|
func saveGuidToHomedir(configuration config) {
|
|
|
|
directory := homedir.Expand() + filepath.Separator + ConfigurationFolderName
|
|
if err := os.Mkdir(directory, ConfigurationFilePermissions); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
configByteContent, err := json.Marshal(configuration)
|
|
if err != nil {
|
|
panic("Something is terribly wrong. I am sorry. I am so so sorry. :~(")
|
|
}
|
|
|
|
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 ?")
|
|
}
|
|
|
|
}
|