package cmd
import (
+ "bufio"
"errors"
"fmt"
"os"
+ Helper "scientia/lib"
+
"github.com/kirsle/configdir"
"github.com/spf13/cobra"
- Helper "scientia/lib"
+ "gopkg.in/yaml.v3"
)
/**
* along with this program. If not, see http://www.sun.com/cddl/cddl.html
*/
+var configPath = configdir.LocalConfig("scientia")
+var configFile = configPath + "/.scientia.yaml"
+
func init() {
configCmd.AddCommand(configInitCmd)
configCmd.AddCommand(configReadCmd)
}
-// The Config file struct
-type Config struct {
- Endpoint struct {
- Host string `yaml:"host"`
- Secret string `yaml:"secret"`
- } `yaml:"endpoint"`
-}
+// INIT config file
-var configInitCmd = &cobra.Command{
+var configInitCmd = &cobra.Command {
Use: "init",
Short: "Initialize config",
Long: `Read, edit and initialize scientia configuration`,
},
}
+// initConfig which creates the default config file
func initConfig() {
- configPath := configdir.LocalConfig("scientia")
err := configdir.MakePath(configPath) // Ensure it exists.
Helper.ErrorCheck(err, "No $HOME/.config/scientia directory available?")
- var configFile = configPath + "/.scientia.yaml"
if FlagDebug {
- fmt.Printf("Local user config path: %s\n", configPath)
- fmt.Printf("Local user config file: %s\n", configFile)
+ fmt.Printf("DEBUG Local user config path: %s\n", configPath)
+ fmt.Printf("DEBUG Local user config file: %s\n", configFile)
}
if _, err := os.Stat(configFile); errors.Is(err, os.ErrNotExist) {
fmt.Fprintf(newConfig, "endpoint:\n")
fmt.Fprintf(newConfig, " host: http://your-scientia-endpoi.nt/api.php\n")
fmt.Fprintf(newConfig, " secret: %s\n", Helper.RandStringBytes(50))
-
} else {
fmt.Printf("Config file exists.: %s \n", configFile)
fmt.Println("Use 'read' to display or 'edit' to modify the config file.")
}
}
-var configReadCmd = &cobra.Command{
+// READ config file
+
+var configReadCmd = &cobra.Command {
Use: "read",
Short: "Read config file",
Long: "Read the config file and print it to stdout",
},
}
+// readConfig does read the existing config file and prints it contents and validates the yaml.
func readConfig() {
+ if FlagDebug {
+ fmt.Printf("DEBUG Local user config path: %s\n", configPath)
+ fmt.Printf("DEBUG Local user config file: %s\n", configFile)
+ }
+ existingConfigFile, err := os.Open(configFile)
+ Helper.ErrorCheck(err, "Can not open config file. Did you create one with 'config init'?")
+ defer existingConfigFile.Close()
+
+ if FlagVerbose {
+ fmt.Printf("Reading config file: %s \n", configFile)
+ }
+
+ // make sure it can be parsed and thus it is valid
+ var decoder = yaml.NewDecoder(existingConfigFile)
+ err = decoder.Decode(&ScientiaConfig)
+ Helper.ErrorCheck(err, "Can not parse config file")
+
+ // just display the contents
+ existingConfigFile.Seek(0,0) // reset needed
+ configBuffer := bufio.NewReader(existingConfigFile)
+ for {
+ line, _, err := configBuffer.ReadLine()
+ if len(line) > 0 {
+ fmt.Println(string(line))
+ }
+ if err != nil {
+ break
+ }
+ }
}
go 1.21.10
require (
- github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
+ github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
+ github.com/spf13/cobra v1.8.1
+ gopkg.in/yaml.v2 v2.4.0
+ gopkg.in/yaml.v3 v3.0.1
+)
+
+require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
- github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f // indirect
- github.com/russross/blackfriday/v2 v2.1.0 // indirect
- github.com/spf13/cobra v1.8.1 // indirect
+ github.com/k0kubun/pp/v3 v3.2.0 // indirect
+ github.com/mattn/go-colorable v0.1.13 // indirect
+ github.com/mattn/go-isatty v0.0.16 // indirect
github.com/spf13/pflag v1.0.5 // indirect
- gopkg.in/yaml.v3 v3.0.1 // indirect
+ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
+ golang.org/x/text v0.3.7 // indirect
)
-github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
+github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs=
+github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
-github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
+github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
+github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
+github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
+github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
+golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
+golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
+gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=