--- /dev/null
+# scienta go cli client
+
+This is a terminal cli client written in go to be used with scientia
+https://://www.bananas-playground.net/projekt/scientia/
+
+!WARNING!
+This is a very simple, with limited experience written, go program.
+Use at own risk and feel free to improve.
+
+Currently only tested on linux.
+
+# Howto build
+
+Nothing special, just use the provided Makefile or directly `go build -o scientia` to use your current os/arch settings.
+
+Or use the Makefile with just `make` to build the binary.
+
+# Usage
+
+At first usage you need to create the config and the individual secret.
+Run `$ scientia config init` to create the default config file.
+The path to the config file is printed. Use `scientia config edit` to edit at least the host setting.
+Update your server it with the secret, which is randomly created.
+
+## Create
+
+Read from a file `$ scientia file.txt` or piped `$ cat file.txt | scientia-cli`
+
+# Commandline arguments
+
+```
+Usage:
+ scientia [flags]
+ scientia [command]
+
+Available Commands:
+ config Modify config
+ help Help about any command
+
+Flags:
+ -d, --debug debug output
+ -h, --help help for scientia
+ -v, --verbose verbose output
+
+Use "scientia [command] --help" for more information about a command.
+```
"bufio"
"errors"
"fmt"
- "os"
-
- Helper "scientia/lib"
-
"github.com/kirsle/configdir"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
+ "log"
+ "os"
+ "os/exec"
+ Helper "scientia/lib"
)
/**
* 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)
+ configCmd.AddCommand(configEditCmd)
}
// INIT config file
// initConfig which creates the default config file
func initConfig() {
- err := configdir.MakePath(configPath) // Ensure it exists.
+ err := configdir.MakePath(ScientiaConfigPath) // Ensure it exists.
Helper.ErrorCheck(err, "No $HOME/.config/scientia directory available?")
if FlagDebug {
- fmt.Printf("DEBUG Local user config path: %s\n", configPath)
- fmt.Printf("DEBUG Local user config file: %s\n", configFile)
+ fmt.Printf("DEBUG Local user config path: %s\n", ScientiaConfigPath)
+ fmt.Printf("DEBUG Local user config file: %s\n", ScientiaConfigPath)
}
- if _, err := os.Stat(configFile); errors.Is(err, os.ErrNotExist) {
- fmt.Printf("Creating new default config file: %s \n", configFile)
+ if _, err := os.Stat(ScientiaConfigFile); errors.Is(err, os.ErrNotExist) {
+ fmt.Printf("Creating new default config file: %s \n", ScientiaConfigFile)
- newConfig, err := os.Create(configFile)
+ newConfig, err := os.Create(ScientiaConfigFile)
Helper.ErrorCheck(err, "Can not create config file!")
defer newConfig.Close()
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))
+
+ fmt.Println("Created a new default config file. Please use the edit command to update it with your settings.")
+
} else {
- fmt.Printf("Config file exists.: %s \n", configFile)
+ fmt.Printf("Config file exists.: %s \n", ScientiaConfigFile)
fmt.Println("Use 'read' to display or 'edit' to modify the config file.")
}
}
// 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)
+ fmt.Printf("DEBUG Local user config path: %s\n", ScientiaConfigPath)
+ fmt.Printf("DEBUG Local user config file: %s\n", ScientiaConfigFile)
}
- existingConfigFile, err := os.Open(configFile)
+ existingConfigFile, err := os.Open(ScientiaConfigFile)
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)
+ fmt.Printf("Reading config file: %s \n", ScientiaConfigFile)
}
// make sure it can be parsed and thus it is valid
}
}
}
+
+// EDIT config file
+
+var configEditCmd = &cobra.Command {
+ Use: "edit",
+ Short: "Edit config file",
+ Long: "Edit the config file with $VISUAL > $EDITOR",
+ Run: func(cmd *cobra.Command, args []string) {
+ editConfig()
+ },
+}
+
+func editConfig() {
+ // default editor
+ var editor = "vim"
+
+ if e := os.Getenv("VISUAL"); e != "" {
+ editor = e
+ } else if e := os.Getenv("EDITOR"); e != "" {
+ editor = e
+ }
+
+ if FlagDebug {
+ fmt.Printf("DEBUG Local user config path: %s\n", ScientiaConfigPath)
+ fmt.Printf("DEBUG Local user config file: %s\n", ScientiaConfigFile)
+ fmt.Printf("DEBUG Using editor: %s\n", editor)
+ }
+
+ if _, err := os.Stat(ScientiaConfigFile); errors.Is(err, os.ErrNotExist) {
+ log.Fatal("Config file missing.");
+ }
+
+ cmd := exec.Command(editor, ScientiaConfigFile)
+ cmd.Stdin = os.Stdin
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ err := cmd.Start()
+ Helper.ErrorCheck(err, "Can not open config file")
+
+ fmt.Println("Waiting for command to finish...")
+ err = cmd.Wait()
+ Helper.ErrorCheck(err, "Command finished with error")
+ fmt.Println("Done.")
+}