@@ -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
109113func 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" )
0 commit comments