@@ -21,7 +21,6 @@ import (
21
21
"fmt"
22
22
"io"
23
23
"net/http"
24
- "os"
25
24
"strings"
26
25
"vimbin/internal/config"
27
26
"vimbin/internal/utils"
@@ -49,68 +48,73 @@ Examples:
49
48
Run : func (cmd * cobra.Command , args []string ) {
50
49
// Check if at least one character is provided
51
50
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." )
54
52
}
55
53
56
54
url := strings .TrimSuffix (config .App .Server .Api .Address , "/" )
57
55
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" )
60
62
}
61
63
62
64
// Concatenate input arguments into a single string
63
- body := strings .Join (args , "\n " )
65
+ input := strings .Join (args , "\n " )
64
66
65
67
// Build the URL based on the "append" flag
66
68
if appendFlag {
67
69
url += "/append"
68
- body = "\n " + body
70
+ input = "\n " + input
69
71
} else {
70
72
url += "/save"
71
73
}
72
74
73
75
// Prepare content for the POST request
74
- content := map [string ]string {"content" : body }
76
+ content := map [string ]string {"content" : input }
75
77
requestBody , err := json .Marshal (content )
76
78
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 )
79
80
}
80
81
81
82
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 )
82
90
83
91
// 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 )
85
93
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 )
88
95
}
89
96
defer response .Body .Close ()
90
97
91
98
// Check for successful response
92
99
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 )
95
101
}
96
102
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 )
100
104
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 )
103
106
}
104
107
105
- fmt .Println (responseBodyBuffer .String ())
108
+ // Print the content to the console
109
+ fmt .Println (string (body ))
106
110
},
107
111
}
108
112
109
113
func init () {
110
- // Add 'fetchCmd ' to the root command
114
+ // Add 'pullCmd ' to the root command
111
115
rootCmd .AddCommand (pushCmd )
112
116
113
- // Define command-line flags for 'fetchCmd '
117
+ // Define command-line flags for 'pullCmd '
114
118
pushCmd .PersistentFlags ().StringVarP (& config .App .Server .Api .Address , "url" , "u" , "" , "The URL of the vimbin server" )
115
119
pushCmd .PersistentFlags ().BoolVarP (& config .App .Server .Api .SkipInsecureVerify , "insecure-skip-verify" , "i" , false , "Skip TLS certificate verification" )
116
120
pushCmd .PersistentFlags ().BoolVarP (& appendFlag , "append" , "a" , false , "Append content to the existing content" )
0 commit comments