Skip to content

Commit 9e98ee6

Browse files
committed
improve config, token and fn
1 parent 0b3a60d commit 9e98ee6

File tree

7 files changed

+51
-19
lines changed

7 files changed

+51
-19
lines changed

.vimbin.yaml

Lines changed: 0 additions & 4 deletions
This file was deleted.

README.md

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88

99
## Commands
1010

11+
### Global Flags
12+
13+
| Flag | Description |
14+
| :---------------------- | :----------------------------------------------------------------------------- |
15+
| `-c`, `--config` `PATH` | Path to the configuration file (Default: `$HOME/.vimbin.yaml`). |
16+
| `--debug` | Activates debug output for detailed logging. |
17+
| `-t`, `--token` `TOKEN` | Token to use for authentication. If not set, a random token will be generated. Can also be set with the environment variable `VIMBIN_TOKEN` |
18+
| `--trace` | Enables trace mode. This will show the content in the logs! |
19+
| `-v`, `--version` | Print version and exit. |
20+
1121
### Serve
1222

1323
Start the server:
@@ -16,12 +26,15 @@ Start the server:
1626
./vimbin serve
1727
```
1828

19-
#### Options
29+
**Flags:**
2030

21-
`--listen-address`, `-a`: The address to listen on for HTTP requests. (Default: `:8080`)
22-
`--theme`, `-t`: The theme to use. Can be auto, light, or dark. (Default: `auto`)
23-
`--directory`, `-d`: The path to the storage directory. Defaults to the current working directory.
24-
`--name`, `-n`: The name of the file to save. (Default: `.vimbin`)
31+
| Flag | Description |
32+
| :-------------------------------------- | :----------------------------------------------------------------------------------------------- |
33+
| `-d`, `--directory` `DIRECTORY` | The path to the storage directory. Defaults to the current working directory. (default `$(pwd)`) |
34+
| `-a`, `--listen-address` `ADDRESS:PORT` | The address to listen on for HTTP requests. (default `:8080`) |
35+
| `-n`, `--name` string | The name of the file to save. (default ".vimbin") |
36+
| `--theme` THEME | The theme to use. Can be `auto`, `light` or `dark`. (default `auto`) |
37+
| `-h`, `--help` | help for serve |
2538

2639
### Push
2740

@@ -31,10 +44,14 @@ Push data to the `vimbin` server:
3144
./vimbin push [text]
3245
```
3346

34-
#### Options
47+
**Flags:**
3548

36-
`--url`, `-u`: The URL of the `vimbin` server.
37-
`--append`, `-a`: Append to the existing file on the `vimbin` server.
49+
| Flag | Description |
50+
| :----------------------------- | :------------------------------------- |
51+
| `-a`, `--append` | Append content to the existing content |
52+
| `-i`, `--insecure-skip-verify` | Skip TLS certificate verification |
53+
| `-u`, `--url` `URL` | The URL of the vimbin server |
54+
| `-h`, `--help` | help for push |
3855

3956
### Fetch
4057

@@ -44,9 +61,13 @@ Fetch the latest data from the `vimbin` server:
4461
./vimbin fetch
4562
```
4663

47-
#### Options
64+
**Flags:**
4865

49-
`--url`, `-u`: The URL of the `vimbin` server.
66+
| Flag | Description |
67+
| :----------------------------- | :-------------------------------- |
68+
| `-i`, `--insecure-skip-verify` | Skip TLS certificate verification |
69+
| `-u`, `--url` `URL` | The URL of the vimbin server |
70+
| `-h`, `--help` | help for fetch |
5071

5172
## Configuration
5273

@@ -61,6 +82,7 @@ server:
6182
theme: auto
6283
api:
6384
address: "http://vimbin.example.com"
85+
token: secure token
6486

6587
storage:
6688
name: .vimbin

cmd/root.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ var rootCmd = &cobra.Command{
7575
zerolog.SetGlobalLevel(zerolog.TraceLevel)
7676
log.Debug().Msgf("Trace output enabled")
7777
}
78+
if token := cmd.Flag("token").Value.String(); token != "" {
79+
config.App.Server.Api.Token.Set(token)
80+
}
7881
},
7982
Run: func(cmd *cobra.Command, args []string) {
8083
// Run is executed if no subcommand is specified.
@@ -104,6 +107,7 @@ func init() {
104107
// Define command-line flags for the root command
105108
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Path to the configuration file (Default: $HOME/.vimbin.yaml).")
106109
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "", false, "Activates debug output for detailed logging.")
110+
rootCmd.PersistentFlags().StringP("token", "t", "", "Token to use for authentication. If not set, a random token will be generated.")
107111
rootCmd.PersistentFlags().BoolVarP(&trace, "trace", "", false, "Enables trace mode. This will show the content in the logs!")
108112
rootCmd.MarkFlagsMutuallyExclusive("debug", "trace") // Ensure that debug and trace flags are mutually exclusive
109113

@@ -113,6 +117,11 @@ func init() {
113117
// initConfig reads the configuration from the specified file or environment variables.
114118
func initConfig() {
115119
if cfgFile != "" {
120+
// Check if the specified config file exists
121+
if _, err := os.Stat(cfgFile); err != nil {
122+
log.Fatal().Msg("Config file '%s' not found")
123+
}
124+
116125
// Use the config file specified by the flag.
117126
viper.SetConfigFile(cfgFile)
118127
} else {

cmd/serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func init() {
7474
// Define command-line flags for the serve command
7575
serveCmd.PersistentFlags().StringVarP(&config.App.Server.Web.Address, "listen-address", "a", ":8080", "The address to listen on for HTTP requests.")
7676

77-
serveCmd.PersistentFlags().StringVarP(&config.App.Server.Web.Theme, "theme", "t", "auto", fmt.Sprintf("The theme to use. Can be %s.", strings.Join(supportedThemes, "|")))
77+
serveCmd.PersistentFlags().StringVarP(&config.App.Server.Web.Theme, "theme", "", "auto", fmt.Sprintf("The theme to use. Can be %s.", strings.Join(supportedThemes, "|")))
7878
serveCmd.RegisterFlagCompletionFunc("theme", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
7979
return supportedThemes, cobra.ShellCompDirectiveDefault
8080
})

internal/config/parse.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func (c *Config) Parse() (err error) {
5050
return fmt.Errorf("Unable to extract hostname and port: %s", err)
5151
}
5252

53+
// Check if the API token was set as ENV variable
54+
if token := os.Getenv("VIMBIN_TOKEN"); token != "" {
55+
c.Server.Api.Token.Set(token)
56+
}
57+
5358
// Check if the API token is valid
5459
if c.Server.Api.Token.Get() == "" {
5560
if err := c.Server.Api.Token.Generate(32); err != nil {

internal/config/read.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/mitchellh/mapstructure"
7+
78
"github.com/spf13/viper"
89
)
910

@@ -23,8 +24,8 @@ func (c *Config) Read(configPath string) error {
2324
viper.SetConfigFile(configPath)
2425

2526
// Read the config file
26-
if err := viper.ReadInConfig(); err != nil {
27-
return fmt.Errorf("Failed to read config file: %v", err)
27+
if err := viper.ReadInConfig(); err == nil {
28+
return nil
2829
}
2930

3031
if err := viper.Unmarshal(c, func(d *mapstructure.DecoderConfig) {

internal/config/utils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ return (--n >= 0) ? (unsigned char) *bufp++ : EOF;
4040
// An error if unable to check or create the storage file.
4141
func checkStorageFile(filePath string) error {
4242
// Open the file to check if it exists
43-
_, err := os.Stat(filePath)
44-
if err != nil {
43+
if _, err := os.Stat(filePath); err != nil {
4544
// If the file doesn't exist, create it with default content
4645
log.Debug().Msg("Storage file not found; creating it with default content")
4746
example := []byte(defaultExample)

0 commit comments

Comments
 (0)