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 (
|
const (
|
||||||
ConfigurationFolderName = ".chub"
|
ConfigurationDirectoryName = ".chub"
|
||||||
ConfigurationFilePermissions = 600
|
ConfigurationFilePermissions = 600
|
||||||
ConfigurationFileName = "config"
|
ConfigurationFileName = "config"
|
||||||
UrlOfTheService = "http://localhost:3000/api/"
|
UrlOfTheService = "http://localhost:3000/api/"
|
||||||
MimeTypeOfService = "application/json"
|
MimeTypeOfService = "application/json"
|
||||||
|
WebSite = "http://confighub.saburly.com"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
30
chub/init.go
30
chub/init.go
@@ -1,14 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/edindazdarevic/confighub/chub/client"
|
"github.com/edindazdarevic/confighub/chub/client"
|
||||||
"github.com/edindazdarevic/confighub/chub/globals"
|
"github.com/edindazdarevic/confighub/chub/globals"
|
||||||
homedir "github.com/mitchellh/go-homedir"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
"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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/edindazdarevic/confighub/chub/globals"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
if len(os.Args[1:]) < 2 {
|
if len(os.Args[1:]) < 1 {
|
||||||
usage()
|
usage()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
command := os.Args[1]
|
command := os.Args[1]
|
||||||
|
if command != "init" && !globals.FileExists(configurationFilePath()) {
|
||||||
|
initializeFirst()
|
||||||
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
|
|
||||||
case command == "init":
|
case command == "init":
|
||||||
|
if len(os.Args[1:]) < 2 {
|
||||||
|
usage()
|
||||||
|
}
|
||||||
machineId := os.Args[2]
|
machineId := os.Args[2]
|
||||||
initCommand(machineId)
|
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("Configuration Hub v0.1")
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
fmt.Println("Usage:")
|
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