Skip to content

Commit

Permalink
Prompt refactor, and command restructuring (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
mellonnen committed Mar 7, 2023
1 parent 1e38ca4 commit 2c1b6f3
Show file tree
Hide file tree
Showing 32 changed files with 1,095 additions and 910 deletions.
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# Ignore all
*

# Unignore all with extensions
!*.*

# Unignore all dirs
!*/

# Unignore Makefile
!Makefile
# Portal log files
.portal-*.log

Expand All @@ -23,4 +34,4 @@
# misc
.DS_Store
dist/
.vscode
.vscode
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ lint:
golangci-lint run --timeout 5m ./...

build:
go build -ldflags=${LINKER_FLAGS} -o portal-bin ./cmd/portal/
go build -ldflags=${LINKER_FLAGS} -o portal ./cmd/portal/

build-production:
CGO=0 go build -ldflags=${LINKER_FLAGS} -o portal ./cmd/portal/
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ portal receive 42-relative-parsec-supernova
#### `Sender` and `Receiver`

- `-r/--relay`: address of the relay server (`:8080`, `myrelay.io:1234`, ...)
- `-s/--tui-style`: the style of the tui (`rich` | `raw`)

#### `Sender`, `Receiver` and `Relay`

Expand All @@ -143,9 +144,11 @@ As evident by the file extension, the config is a simple [YAML](https://yaml.org

#### Default configuration
```yaml
relay: 167.71.65.96:80
relay: portal.spatiumportae.com
verbose: false
prompt_overwrite_files: true
relay_serve_port: 8080
tui_style: rich
```

### Hosting your own relay
Expand Down Expand Up @@ -231,4 +234,4 @@ The public relay available for everyone to use is..
<a href="https://m.do.co/c/73a491fda077">
<img src="https://opensource.nyc3.cdn.digitaloceanspaces.com/attribution/assets/PoweredByDO/DO_Powered_by_Badge_blue.svg" width="201px">
</a>
</p>
</p>
92 changes: 92 additions & 0 deletions cmd/portal/commands/config.go
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
}
33 changes: 33 additions & 0 deletions cmd/portal/commands/helpers.go
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
}
Loading

0 comments on commit 2c1b6f3

Please sign in to comment.