Skip to content

Commit e52c9e9

Browse files
committed
rename fetch to pull & fix commands pull & push
1 parent 22d3dd3 commit e52c9e9

File tree

8 files changed

+76
-45
lines changed

8 files changed

+76
-45
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ Push data to the `vimbin` server:
5353
| `-u`, `--url` `URL` | The URL of the vimbin server |
5454
| `-h`, `--help` | help for push |
5555

56-
### Fetch
56+
### Pull
5757

58-
Fetch the latest data from the `vimbin` server:
58+
Pull the latest data from the `vimbin` server:
5959

6060
```bash
61-
./vimbin fetch
61+
./vimbin pull
6262
```
6363

6464
**Flags:**

cmd/fetch.go cmd/pull.go

+33-18
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,64 @@ package cmd
1818
import (
1919
"fmt"
2020
"io"
21-
"os"
21+
"net/http"
2222
"strings"
2323
"vimbin/internal/config"
2424
"vimbin/internal/utils"
2525

2626
"github.com/rs/zerolog/log"
27-
2827
"github.com/spf13/cobra"
2928
)
3029

31-
// fetchCmd represents the 'fetch' command for retrieving the latest data from the vimbin server.
32-
var fetchCmd = &cobra.Command{
33-
Use: "fetch",
34-
Short: "Fetches the latest data from the vimbin server",
35-
Long: `The 'fetch' command retrieves the latest content from the vimbin server specified by the provided URL.
30+
// pullCmd represents the 'fetch' command for retrieving the latest data from the vimbin server.
31+
var pullCmd = &cobra.Command{
32+
Use: "pull",
33+
Short: "Pulls the latest data from the vimbin server",
34+
Long: `The 'pull' command retrieves the latest content from the vimbin server specified by the provided URL.
3635
It makes a GET request to the server and prints the response body to the console.
3736
3837
Example:
39-
vimbin fetch --url http://example.com`,
38+
vimbin pull --url http://example.com`,
4039
Run: func(cmd *cobra.Command, args []string) {
4140
url := strings.TrimSuffix(config.App.Server.Api.Address, "/")
4241

4342
if url == "" {
44-
log.Error().Msg("URL is empty")
45-
os.Exit(1)
43+
log.Fatal().Msg("URL is empty")
4644
}
4745
url += "/fetch"
46+
log.Debug().Msgf("URL: %s", url)
47+
48+
apiToken := config.App.Server.Api.Token.Get()
49+
if apiToken == "" {
50+
log.Fatal().Msg("API token is empty")
51+
}
4852

4953
httpClient := utils.CreateHTTPClient(config.App.Server.Api.SkipInsecureVerify)
54+
req, err := http.NewRequest("GET", url, nil)
55+
if err != nil {
56+
log.Fatal().Msgf("Error creating HTTP request: %v", err)
57+
}
58+
59+
req.Header.Set("Content-Type", "application/json")
60+
req.Header.Set("X-API-Token", apiToken)
5061

5162
// Make a GET request to the vimbin server
52-
response, err := httpClient.Get(url)
63+
response, err := httpClient.Do(req)
5364
if err != nil {
5465
log.Error().Msgf("Error making GET request: %s", err)
5566
return
5667
}
5768
defer response.Body.Close()
5869

70+
// Check for successful response
71+
if response.StatusCode != http.StatusOK {
72+
log.Fatal().Msgf("Unexpected status code %d", response.StatusCode)
73+
}
74+
5975
// Read the response body
6076
body, err := io.ReadAll(response.Body)
6177
if err != nil {
62-
log.Error().Msgf("Error reading response body: %s", err)
63-
return
78+
log.Fatal().Msgf("Error reading response body: %s", err)
6479
}
6580

6681
// Print the content to the console
@@ -69,10 +84,10 @@ Example:
6984
}
7085

7186
func init() {
72-
// Add 'fetchCmd' to the root command
73-
rootCmd.AddCommand(fetchCmd)
87+
// Add 'pullCmd' to the root command
88+
rootCmd.AddCommand(pullCmd)
7489

75-
// Define command-line flags for 'fetchCmd'
76-
fetchCmd.PersistentFlags().StringVarP(&config.App.Server.Api.Address, "url", "u", "", "The URL of the vimbin server")
77-
fetchCmd.PersistentFlags().BoolVarP(&config.App.Server.Api.SkipInsecureVerify, "insecure-skip-verify", "i", false, "Skip TLS certificate verification")
90+
// Define command-line flags for 'pullCmd'
91+
pullCmd.PersistentFlags().StringVarP(&config.App.Server.Api.Address, "url", "u", "", "The URL of the vimbin server")
92+
pullCmd.PersistentFlags().BoolVarP(&config.App.Server.Api.SkipInsecureVerify, "insecure-skip-verify", "i", false, "Skip TLS certificate verification")
7893
}

cmd/push.go

+27-23
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"io"
2323
"net/http"
24-
"os"
2524
"strings"
2625
"vimbin/internal/config"
2726
"vimbin/internal/utils"
@@ -49,68 +48,73 @@ Examples:
4948
Run: func(cmd *cobra.Command, args []string) {
5049
// Check if at least one character is provided
5150
if len(args) < 1 {
52-
log.Error().Msg("You must push at least one character.")
53-
os.Exit(1)
51+
log.Fatal().Msg("You must push at least one character.")
5452
}
5553

5654
url := strings.TrimSuffix(config.App.Server.Api.Address, "/")
5755
if url == "" {
58-
log.Error().Msg("URL is empty")
59-
os.Exit(1)
56+
log.Fatal().Msg("URL is empty")
57+
}
58+
59+
apiToken := config.App.Server.Api.Token.Get()
60+
if apiToken == "" {
61+
log.Fatal().Msg("API token is empty")
6062
}
6163

6264
// Concatenate input arguments into a single string
63-
body := strings.Join(args, "\n")
65+
input := strings.Join(args, "\n")
6466

6567
// Build the URL based on the "append" flag
6668
if appendFlag {
6769
url += "/append"
68-
body = "\n" + body
70+
input = "\n" + input
6971
} else {
7072
url += "/save"
7173
}
7274

7375
// Prepare content for the POST request
74-
content := map[string]string{"content": body}
76+
content := map[string]string{"content": input}
7577
requestBody, err := json.Marshal(content)
7678
if err != nil {
77-
log.Error().Msgf("Error encoding JSON: %s", err)
78-
os.Exit(1)
79+
log.Fatal().Msgf("Error encoding JSON: %s", err)
7980
}
8081

8182
httpClient := utils.CreateHTTPClient(config.App.Server.Api.SkipInsecureVerify)
83+
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
84+
if err != nil {
85+
log.Fatal().Msgf("Error creating HTTP request: %v", err)
86+
}
87+
88+
req.Header.Set("Content-Type", "application/json")
89+
req.Header.Set("X-API-Token", apiToken)
8290

8391
// Make the POST request to the vimbin server
84-
response, err := httpClient.Post(url, "application/json", bytes.NewBuffer(requestBody))
92+
response, err := httpClient.Do(req)
8593
if err != nil {
86-
log.Error().Msgf("Error making POST request: %s", err)
87-
os.Exit(1)
94+
log.Fatal().Msgf("Error making POST request: %s", err)
8895
}
8996
defer response.Body.Close()
9097

9198
// Check for successful response
9299
if response.StatusCode != http.StatusOK {
93-
log.Error().Msgf("Unexpected status code %d", response.StatusCode)
94-
os.Exit(1)
100+
log.Fatal().Msgf("Unexpected status code %d", response.StatusCode)
95101
}
96102

97-
// Read and print the response body
98-
var responseBodyBuffer bytes.Buffer
99-
_, err = io.Copy(&responseBodyBuffer, response.Body)
103+
body, err := io.ReadAll(response.Body)
100104
if err != nil {
101-
log.Error().Msgf("Error reading response body: %s", err)
102-
os.Exit(1)
105+
log.Fatal().Msgf("Error reading response body: %s", err)
103106
}
104107

105-
fmt.Println(responseBodyBuffer.String())
108+
// Print the content to the console
109+
fmt.Println(string(body))
106110
},
107111
}
108112

109113
func init() {
110-
// Add 'fetchCmd' to the root command
114+
// Add 'pullCmd' to the root command
111115
rootCmd.AddCommand(pushCmd)
112116

113-
// Define command-line flags for 'fetchCmd'
117+
// Define command-line flags for 'pullCmd'
114118
pushCmd.PersistentFlags().StringVarP(&config.App.Server.Api.Address, "url", "u", "", "The URL of the vimbin server")
115119
pushCmd.PersistentFlags().BoolVarP(&config.App.Server.Api.SkipInsecureVerify, "insecure-skip-verify", "i", false, "Skip TLS certificate verification")
116120
pushCmd.PersistentFlags().BoolVarP(&appendFlag, "append", "a", false, "Append content to the existing content")

cmd/root.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
)
3131

3232
const (
33-
version = "v0.0.14"
33+
version = "v0.0.16"
3434
)
3535

3636
var (
@@ -53,6 +53,9 @@ var rootCmd = &cobra.Command{
5353
5454
- push: Quickly send text to the vimbin server from the command line. This allows for easy integration
5555
with other tools and scripts, streamlining the process of sharing content through vimbin.
56+
57+
- pull: Retrieve the latest content from the vimbin server. This allows for easy integration with other
58+
tools and scripts, streamlining the process of retrieving content from vimbin.
5659
`,
5760
PersistentPreRun: func(cmd *cobra.Command, args []string) {
5861
// PersistentPreRun is executed before any subcommand is executed.
@@ -65,6 +68,7 @@ var rootCmd = &cobra.Command{
6568

6669
// Configure zerolog
6770
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
71+
zerolog.SetGlobalLevel(zerolog.InfoLevel)
6872
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
6973
log.Logger = zerolog.New(output).With().Timestamp().Logger()
7074

@@ -85,9 +89,13 @@ var rootCmd = &cobra.Command{
8589
log.Debug().Msgf("Trace output enabled")
8690
}
8791

92+
// Save token to config
8893
if token := cmd.Flag("token").Value.String(); token != "" {
8994
config.App.Server.Api.Token.Set(token)
9095
}
96+
97+
config.App.Version = version
98+
log.Debug().Msgf("Version: %s", config.App.Version)
9199
},
92100
Run: func(cmd *cobra.Command, args []string) {
93101
// Run is executed if no subcommand is specified.

internal/config/structs.go

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var App Config
1111

1212
// Config represents the application configuration.
1313
type Config struct {
14+
Version string `mapstructure:"-"` // Version is the version of the application.
1415
HtmlTemplate *template.Template `mapstructure:"-"` // HtmlTemplate contains the HTML template content.
1516
Server Server `mapstructure:"server"` // Server represents the server configuration.
1617
Storage Storage `mapstructure:"storage"` // Storage represents the storage configuration.

internal/handlers/home.go

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func Home(w http.ResponseWriter, r *http.Request) {
3131
Content: config.App.Storage.Content.Get(),
3232
Token: config.App.Server.Api.Token.Get(),
3333
Theme: config.App.Server.Web.Theme,
34+
Version: config.App.Version,
3435
}
3536

3637
if err := config.App.HtmlTemplate.Execute(w, page); err != nil {

internal/handlers/structs.go

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ type Page struct {
99
Content string // Content is the content of the page.
1010
Token string // Token is the API token.
1111
Theme string // Theme is the theme of the page.
12+
Version string // Version is the version of the application.
1213
}

web/templates/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta name="version" content="{{.Version}}" />
67

78
<title>vimbin - a pastebin with vim motion</title>
89
<meta charset="utf-8" />

0 commit comments

Comments
 (0)