Skip to content

Commit f73c94f

Browse files
committed
Send CLI user-agent and print messages
1 parent 8ff5916 commit f73c94f

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

api.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package yaakcli
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"github.com/pterm/pterm"
67
"io"
78
"net/http"
@@ -30,6 +31,7 @@ func SendAPIRequest(r *http.Request) []byte {
3031
}
3132

3233
r.Header.Set("X-Yaak-Session", token)
34+
r.Header.Set("User-Agent", fmt.Sprintf("YaakCli/%s (%s)", CLIVersion, GetUAPlatform()))
3335

3436
client := &http.Client{Timeout: 10 * time.Second}
3537
resp, err := client.Do(r)
@@ -52,6 +54,11 @@ func SendAPIRequest(r *http.Request) []byte {
5254
os.Exit(1)
5355
}
5456

57+
message := resp.Header.Get("X-Cli-Message")
58+
if message != "" {
59+
pterm.Info.Printf(message)
60+
}
61+
5562
return body
5663
}
5764

cmd_root.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import (
55
"os"
66
)
77

8-
func rootCmd(version string) *cobra.Command {
8+
var CLIVersion string
9+
10+
func rootCmd(v string) *cobra.Command {
11+
CLIVersion = v
912
var fVersion bool
1013
cmd := &cobra.Command{
1114
Use: "yaakcli",
1215
Short: "Develop plugins for Yaak",
1316
Long: "Generate, build, and debug plugins for Yaak, the most intuitive desktop API client",
1417
Run: func(cmd *cobra.Command, args []string) {
1518
if fVersion {
16-
println(version)
19+
println(CLIVersion)
1720
os.Exit(0)
1821
}
1922

util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package yaakcli
33
import (
44
"os"
55
"path"
6+
"runtime"
67
"strings"
78
)
89

@@ -39,3 +40,16 @@ func prodStagingDevStr(prod, staging, dev string) string {
3940
return prod
4041
}
4142
}
43+
44+
func GetUAPlatform() string {
45+
switch runtime.GOOS {
46+
case "windows":
47+
return "Win"
48+
case "darwin":
49+
return "Mac"
50+
case "linux":
51+
return "Linux"
52+
default:
53+
return "Unknown"
54+
}
55+
}

0 commit comments

Comments
 (0)