Implement temporary "init" functionality

This commit is contained in:
2024-03-07 22:12:38 -05:00
parent aa1098d472
commit c6ee5fff06
8 changed files with 111 additions and 8 deletions
+2 -1
View File
@@ -153,7 +153,8 @@ func main() {
offline.ClipClearArgument() offline.ClipClearArgument()
case "sync": case "sync":
cli.SshypSync() // TODO Remove after native sync is implemented cli.SshypSync() // TODO Remove after native sync is implemented
case "init": // TODO offline.Init(), exit after run case "init":
cli.TempInitCli()
case "tweak": // TODO offline.Tweak(), exit after run case "tweak": // TODO offline.Tweak(), exit after run
case "add": case "add":
cli.HelpAdd() cli.HelpAdd()
+1 -5
View File
@@ -88,16 +88,12 @@ func EntryListGen() {
// check for errors encountered while walking directory // check for errors encountered while walking directory
if err != nil { if err != nil {
// create EntryRoot if the error is the result of it not existing on the system
if os.IsNotExist(err) { if os.IsNotExist(err) {
_ = os.Mkdir(offline.EntryRoot, 0700) fmt.Println(offline.AnsiError + "The entry directory does not exist - run \"mutn init\" to create it" + offline.AnsiReset)
dirList = append(dirList, "")
} else { } else {
// otherwise, print the source of the error // otherwise, print the source of the error
fmt.Println(offline.AnsiError + "An unexpected error occurred while generating the entry list: " + err.Error() + offline.AnsiReset) fmt.Println(offline.AnsiError + "An unexpected error occurred while generating the entry list: " + err.Error() + offline.AnsiReset)
} }
// quit walking EntryRoot and return nil to allow the program to continue
return nil
} }
// trim root path from each path before storing // trim root path from each path before storing
+20
View File
@@ -0,0 +1,20 @@
package cli
import (
"github.com/rwinkhart/MUTN/src/offline"
"os"
"os/exec"
)
func TempInitCli() {
// gpgID
cmd := exec.Command("gpg", "-k")
cmd.Stdout = os.Stdout
cmd.Run()
gpgID := input("GPG key ID:")
// textEditor
textEditor := input("Text editor (leave blank for $EDITOR, falls back to \"vi\"):")
offline.TempInit(map[string]string{"textEditor": textEditor, "gpgID": gpgID})
}
+2 -1
View File
@@ -4,7 +4,8 @@ package offline
// EntryRoot path to libmutton entry directory // EntryRoot path to libmutton entry directory
var EntryRoot = home + "/.local/share/libmutton" var EntryRoot = home + "/.local/share/libmutton"
var ConfigPath = home + "/.config/libmutton/libmutton.ini" var ConfigDir = home + "/.config/libmutton"
var ConfigPath = ConfigDir + "/libmutton.ini"
// PathSeparator defines the character used to separate directories in a path (platform-specific) // PathSeparator defines the character used to separate directories in a path (platform-specific)
const PathSeparator = "/" const PathSeparator = "/"
+2 -1
View File
@@ -4,7 +4,8 @@ package offline
// EntryRoot path to libmutton entry directory // EntryRoot path to libmutton entry directory
var EntryRoot = home + "\\AppData\\Local\\libmutton\\entries" var EntryRoot = home + "\\AppData\\Local\\libmutton\\entries"
var ConfigPath = home + "\\AppData\\Local\\libmutton\\libmutton.ini" var ConfigDir = home + "\\AppData\\Local\\libmutton"
var ConfigPath = ConfigDir + "\\libmutton.ini"
// PathSeparator defines the character used to separate directories in a path (platform-specific) // PathSeparator defines the character used to separate directories in a path (platform-specific)
const PathSeparator = "\\" const PathSeparator = "\\"
+39
View File
@@ -0,0 +1,39 @@
package offline
import (
"fmt"
"os"
)
func TempInit(configFileMap map[string]string) {
// create EntryRoot and ConfigDir
dirInit()
// remove existing config file
err := os.Remove(ConfigPath)
if err != nil {
// ignore error if file does not exist
if !os.IsNotExist(err) {
fmt.Println(AnsiError + "Failed to remove existing libmutton.ini:" + err.Error() + AnsiReset)
}
}
// ensure textEditor is set
if configFileMap["textEditor"] == "" {
textEditor := os.Getenv("EDITOR")
if textEditor == "" {
textEditor = fallbackEditor
}
configFileMap["textEditor"] = textEditor
}
// create and write config file
configFile, _ := os.OpenFile(ConfigPath, os.O_CREATE|os.O_WRONLY, 0600)
defer configFile.Close()
configFile.WriteString("[LIBMUTTON]\n")
for key, value := range configFileMap {
configFile.WriteString(key + " = " + value + "\n")
}
os.Exit(0)
}
+26
View File
@@ -0,0 +1,26 @@
//go:build !windows
package offline
import (
"fmt"
"os"
)
const fallbackEditor = "vi" // vi is pre-installed on most UNIX systems
func dirInit() {
// create EntryRoot
err := os.MkdirAll(EntryRoot, 0700)
if err != nil {
fmt.Println(AnsiError + "Failed to create \"" + EntryRoot + "\":" + err.Error() + AnsiReset)
os.Exit(1)
}
// create config directory
err = os.MkdirAll(ConfigDir, 0700)
if err != nil {
fmt.Println(AnsiError + "Failed to create \"" + ConfigDir + "\":" + err.Error() + AnsiReset)
os.Exit(1)
}
}
+19
View File
@@ -0,0 +1,19 @@
//go:build windows
package offline
import (
"fmt"
"os"
)
const fallbackEditor = "nvim" // since there is no pre-installed CLI editor on Windows, default to the most popular one
func dirInit() {
// create EntryRoot (includes config directory on Windows)
err := os.MkdirAll(EntryRoot, 0700)
if err != nil {
fmt.Println(AnsiError + "Failed to create \"" + EntryRoot + "\":" + err.Error() + AnsiReset)
os.Exit(1)
}
}