-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prompt refactor, and command restructuring (#58)
- Loading branch information
Showing
32 changed files
with
1,095 additions
and
910 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/SpatiumPortae/portal/cmd/portal/config" | ||
"github.com/alecthomas/chroma/quick" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func Config() *cobra.Command { | ||
|
||
pathCmd := &cobra.Command{ | ||
Use: "path", | ||
Short: "Output the path of the config file", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println(viper.ConfigFileUsed()) | ||
}, | ||
} | ||
|
||
viewCmd := &cobra.Command{ | ||
Use: "view", | ||
Short: "View the configured options", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
configPath := viper.ConfigFileUsed() | ||
config, err := os.ReadFile(configPath) | ||
if err != nil { | ||
return fmt.Errorf("config file (%s) could not be read: %w", configPath, err) | ||
} | ||
if err := quick.Highlight(os.Stdout, string(config), "yaml", "terminal256", "onedark"); err != nil { | ||
// Failed to highlight output, output un-highlighted config file contents. | ||
fmt.Println(string(config)) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
editCmd := &cobra.Command{ | ||
Use: "edit", | ||
Short: "Edit the configuration file", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
configPath := viper.ConfigFileUsed() | ||
// Strip arguments from editor variable -- allows exec.Command to lookup the editor executable correctly. | ||
editor, _, _ := strings.Cut(os.Getenv("EDITOR"), " ") | ||
if len(editor) == 0 { | ||
//lint:ignore ST1005 error string is command output | ||
return fmt.Errorf( | ||
"Could not find default editor (is the $EDITOR variable set?)\nOptionally you can open the file (%s) manually", configPath, | ||
) | ||
} | ||
|
||
editorCmd := exec.Command(editor, configPath) | ||
editorCmd.Stdin = os.Stdin | ||
editorCmd.Stdout = os.Stdout | ||
editorCmd.Stderr = os.Stderr | ||
if err := editorCmd.Run(); err != nil { | ||
return fmt.Errorf("failed to open file (%s) in editor (%s): %w", configPath, editor, err) | ||
} | ||
return nil | ||
}, | ||
} | ||
resetCmd := &cobra.Command{ | ||
Use: "reset", | ||
Short: "Reset to the default configuration", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
configPath := viper.ConfigFileUsed() | ||
err := os.WriteFile(configPath, config.GetDefault().Yaml(), 0) | ||
if err != nil { | ||
return fmt.Errorf("config file (%s) could not be read/written to: %w", configPath, err) | ||
} | ||
return nil | ||
}, | ||
} | ||
configCmd := &cobra.Command{ | ||
Use: "config", | ||
Short: "View and configure options", | ||
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), | ||
ValidArgs: []string{pathCmd.Name(), viewCmd.Name(), editCmd.Name(), resetCmd.Name()}, | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
} | ||
|
||
configCmd.AddCommand(pathCmd) | ||
configCmd.AddCommand(viewCmd) | ||
configCmd.AddCommand(editCmd) | ||
configCmd.AddCommand(resetCmd) | ||
|
||
return configCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
relayFlagDesc = `Address of relay server. Accepted formats: | ||
- 127.0.0.1:8080 | ||
- [::1]:8080 | ||
- somedomain.com/relay | ||
- ... | ||
` | ||
tuiStyleFlagDesc = "Style of the tui (rich|raw)" | ||
) | ||
|
||
func setupLoggingFromViper(cmd string) (*os.File, error) { | ||
if viper.GetBool("verbose") { | ||
f, err := tea.LogToFile(fmt.Sprintf(".portal-%s.log", cmd), fmt.Sprintf("portal-%s: \n", cmd)) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not log to the provided file: %w", err) | ||
} | ||
return f, nil | ||
} | ||
log.SetOutput(io.Discard) | ||
return nil, nil | ||
} |
Oops, something went wrong.