Repository navigation
Expand file tree
/
Copy pathcmd.go
More file actions
101 lines (82 loc) · 2.57 KB
/
Copy pathcmd.go
File metadata and controls
101 lines (82 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package cmd
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"io"
"github.com/exercism/cli/config"
"github.com/spf13/viper"
)
var (
// BinaryName is the name of the app.
// By default this is exercism, but people
// are free to name this however they want.
// The usage examples and help strings should reflect
// the actual name of the binary.
BinaryName string
// Out is used to write to information.
Out io.Writer
// Err is used to write errors.
Err io.Writer
)
const msgWelcomePleaseConfigure = `
Welcome to Exercism!
To get started, you need to configure the tool with your API token.
Find your token at
%s
Then run the configure command:
%s configure --token=YOUR_TOKEN
`
// Running configure without any arguments will attempt to
// set the default workspace. If the default workspace directory
// risks clobbering an existing directory, it will print an
// error message that explains how to proceed.
const msgRerunConfigure = `
Please re-run the configure command to define where
to download the exercises.
%s configure
`
const msgMissingMetadata = `
The exercise you are submitting doesn't have the necessary metadata.
Please see https://exercism.io/cli-v1-to-v2 for instructions on how to fix it.
`
// validateUserConfig validates the presence of required user config values
func validateUserConfig(cfg *viper.Viper) error {
if cfg.GetString("token") == "" {
return fmt.Errorf(
msgWelcomePleaseConfigure,
config.SettingsURL(cfg.GetString("apibaseurl")),
BinaryName,
)
}
if cfg.GetString("workspace") == "" || cfg.GetString("apibaseurl") == "" {
return fmt.Errorf(msgRerunConfigure, BinaryName)
}
return nil
}
// decodedAPIError decodes and returns the error message from the API response.
// If the message is blank, it returns a fallback message with the status code.
func decodedAPIError(resp *http.Response) error {
var apiError struct {
Error struct {
Type string `json:"type"`
Message string `json:"message"`
PossibleTrackIDs []string `json:"possible_track_ids"`
} `json:"error,omitempty"`
}
if err := json.NewDecoder(resp.Body).Decode(&apiError); err != nil {
return fmt.Errorf("failed to parse API error response: %s", err)
}
if apiError.Error.Message != "" {
if apiError.Error.Type == "track_ambiguous" {
return fmt.Errorf(
"%s: %s",
apiError.Error.Message,
strings.Join(apiError.Error.PossibleTrackIDs, ", "),
)
}
return fmt.Errorf(apiError.Error.Message)
}
return fmt.Errorf("unexpected API response: %d", resp.StatusCode)
}