Skip to content

Commit

Permalink
Merge pull request #60 from SpatiumPortae/develop
Browse files Browse the repository at this point in the history
Develop -> Master
  • Loading branch information
mellonnen authored Mar 7, 2023
2 parents bacd9ac + 06a65d2 commit 647b8b4
Show file tree
Hide file tree
Showing 38 changed files with 1,419 additions and 916 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Ignore all
*

# Unignore all with extensions
!*.*

# Unignore all dirs
!*/

# Unignore Makefile
!Makefile

# Portal log files
.portal-*.log

Expand Down
13 changes: 7 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Mutli-stage build.
# Multi-stage build.
FROM golang:1.18-alpine3.14 as build-stage

# Copy source code and build binary.
ARG version
# Copy source code and build binary
RUN mkdir /usr/app
COPY . /usr/app
WORKDIR /usr/app
RUN CGO=0 go build -ldflags="-s -X main.version=${version}" -o app ./cmd/portal/
RUN CGO=0 go build -ldflags="-s -X main.version=${version}" -o portal ./cmd/portal/

# Copy binary from build container and build image.
FROM alpine:3.14
RUN mkdir /usr/app
RUN mkdir /usr/app
WORKDIR /usr/app
COPY --from=build-stage /usr/app/app .
COPY --from=build-stage /usr/app/portal .

ENTRYPOINT [ "./app", "serve","-p", "8080" ]
ENTRYPOINT [ "./portal", "serve"]
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 647b8b4

Please sign in to comment.