checking for config file
This commit is contained in:
5
chub/add.go
Normal file
5
chub/add.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func addCommand(pathOfFile string, typeOfFile string) {
|
||||
return
|
||||
}
|
||||
55
chub/config.go
Normal file
55
chub/config.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/edindazdarevic/confighub/chub/globals"
|
||||
homedir "github.com/mitchellh/go-homedir"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
MachineGuid string
|
||||
}
|
||||
|
||||
func configurationDirectoryName() string {
|
||||
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)
|
||||
|
||||
return expandedDirectoryName + globals.RuneToAscii(filepath.Separator) + globals.ConfigurationDirectoryName
|
||||
}
|
||||
|
||||
func configurationFilePath() string {
|
||||
|
||||
return configurationDirectoryName() + globals.RuneToAscii(filepath.Separator) + globals.ConfigurationFileName
|
||||
|
||||
}
|
||||
|
||||
func saveGuidToHomedir(configuration config) {
|
||||
directory := configurationDirectoryName()
|
||||
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(configurationFilePath(), configByteContent, globals.ConfigurationFilePermissions)
|
||||
globals.PanicWithStyle("I cannot write to config file in user directory. How can this be ?", err)
|
||||
}
|
||||
|
||||
func loadGuidFromHomedir() config {
|
||||
result := config{}
|
||||
|
||||
file, err := ioutil.ReadFile(configurationFilePath())
|
||||
globals.PanicWithStyle("I cannot read config file in user directory. How can this be ?", err)
|
||||
|
||||
json.Unmarshal(file, &result)
|
||||
return result
|
||||
|
||||
}
|
||||
@@ -8,11 +8,12 @@ import (
|
||||
|
||||
|
||||
const (
|
||||
ConfigurationFolderName = ".chub"
|
||||
ConfigurationDirectoryName = ".chub"
|
||||
ConfigurationFilePermissions = 600
|
||||
ConfigurationFileName = "config"
|
||||
UrlOfTheService = "http://localhost:3000/api/"
|
||||
MimeTypeOfService = "application/json"
|
||||
WebSite = "http://confighub.saburly.com"
|
||||
)
|
||||
|
||||
|
||||
|
||||
30
chub/init.go
30
chub/init.go
@@ -1,14 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/edindazdarevic/confighub/chub/client"
|
||||
"github.com/edindazdarevic/confighub/chub/globals"
|
||||
homedir "github.com/mitchellh/go-homedir"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
@@ -27,29 +23,3 @@ func initCommand(guid string) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
39
chub/main.go
39
chub/main.go
@@ -2,21 +2,41 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/edindazdarevic/confighub/chub/globals"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
if len(os.Args[1:]) < 2 {
|
||||
if len(os.Args[1:]) < 1 {
|
||||
usage()
|
||||
return
|
||||
}
|
||||
|
||||
command := os.Args[1]
|
||||
if command != "init" && !globals.FileExists(configurationFilePath()) {
|
||||
initializeFirst()
|
||||
}
|
||||
|
||||
switch {
|
||||
|
||||
case command == "init":
|
||||
if len(os.Args[1:]) < 2 {
|
||||
usage()
|
||||
}
|
||||
machineId := os.Args[2]
|
||||
initCommand(machineId)
|
||||
|
||||
case command == "add":
|
||||
if len(os.Args[1:]) < 2 {
|
||||
usage()
|
||||
}
|
||||
pathToConfFile := os.Args[2]
|
||||
typeOfConfFile := "unknown"
|
||||
if len(os.Args[1:]) >= 3 {
|
||||
typeOfConfFile = os.Args[3]
|
||||
}
|
||||
addCommand(pathToConfFile, typeOfConfFile)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,5 +45,18 @@ func usage() {
|
||||
fmt.Println("Configuration Hub v0.1")
|
||||
fmt.Println("")
|
||||
fmt.Println("Usage:")
|
||||
fmt.Println("\tchub init <machine-uuid> - connects your server to the config hub machine id <machine-uiid>")
|
||||
fmt.Println(" chub init <machine-uuid> - connects your server to the config hub machine id <machine-uiid>")
|
||||
fmt.Println(" chub add /path/to/file.conf <type> - starts tracking configuration file on that path")
|
||||
fmt.Println(" This <type> thingy can be one of the following:\n")
|
||||
fmt.Println(" nginx\n elasticsearch\n")
|
||||
fmt.Println(" Or just leave it out and I will try to autodetect it (and sometimes fail)\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func initializeFirst() {
|
||||
fmt.Println("Configuration Hub v0.1")
|
||||
fmt.Println("")
|
||||
fmt.Println("There is no configuration file present. You need to run chub init first!")
|
||||
fmt.Printf("Checkout %v for details!\n", globals.WebSite)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user