Skip to content

Commit fb5a543

Browse files
committed
Move server config string parsing to config package
Config string parsing functions moved to config package and made public to be importable by other services (e.g., gin-doi) which may require using client functions and configurations. These will eventually be moved to libgin.
1 parent 5487350 commit fb5a543

File tree

3 files changed

+63
-44
lines changed

3 files changed

+63
-44
lines changed

ginclient/config/config.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"strconv"
9+
"strings"
810

911
"github.com/G-Node/gin-cli/ginclient/log"
12+
"github.com/G-Node/gin-cli/gincmd/ginerrors"
1013
"github.com/fatih/color"
1114
"github.com/shibukawa/configdir"
1215
"github.com/spf13/viper"
@@ -249,6 +252,54 @@ func Path(create bool) (string, error) {
249252

250253
// Util functions //
251254

255+
// ParseWebString takes a string which contains all information about a
256+
// server's web configuration (e.g., https://gin.g-node.org:443) and returns a
257+
// WebCfg struct.
258+
func ParseWebString(webstring string) (WebCfg, error) {
259+
var webconf WebCfg
260+
errmsg := fmt.Sprintf("invalid web configuration line %s", webstring)
261+
split := strings.SplitN(webstring, "://", 2)
262+
if len(split) != 2 {
263+
return webconf, fmt.Errorf("%s: %s", errmsg, ginerrors.MissingURLScheme)
264+
}
265+
webconf.Protocol = split[0]
266+
267+
split = strings.SplitN(split[1], ":", 2)
268+
if len(split) != 2 {
269+
return webconf, fmt.Errorf(errmsg)
270+
}
271+
port, err := strconv.ParseUint(split[1], 10, 16)
272+
if err != nil {
273+
return webconf, fmt.Errorf("%s: %s", errmsg, ginerrors.BadPort)
274+
}
275+
webconf.Host, webconf.Port = split[0], uint16(port)
276+
return webconf, nil
277+
}
278+
279+
// ParseGitString takes a string which contains all information about a
280+
// server's git configuration (e.g., [email protected]:22) and returns a
281+
// GitCfg struct.
282+
func ParseGitString(gitstring string) (GitCfg, error) {
283+
var gitconf GitCfg
284+
errmsg := fmt.Sprintf("invalid git configuration line %s", gitstring)
285+
split := strings.SplitN(gitstring, "@", 2)
286+
if len(split) != 2 {
287+
return gitconf, fmt.Errorf("%s: %s", errmsg, ginerrors.MissingGitUser)
288+
}
289+
gitconf.User = split[0]
290+
291+
split = strings.SplitN(split[1], ":", 2)
292+
if len(split) != 2 {
293+
return gitconf, fmt.Errorf(errmsg)
294+
}
295+
port, err := strconv.ParseUint(split[1], 10, 16)
296+
if err != nil {
297+
return gitconf, fmt.Errorf("%s: %s", errmsg, ginerrors.BadPort)
298+
}
299+
gitconf.Host, gitconf.Port = split[0], uint16(port)
300+
return gitconf, nil
301+
}
302+
252303
// pathExists returns true if the path exists
253304
func pathExists(path string) bool {
254305
if _, err := os.Stat(path); os.IsNotExist(err) {

gincmd/addservercmd.go

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

33
import (
44
"fmt"
5-
"strconv"
65
"strings"
76

87
"github.com/G-Node/gin-cli/ginclient/config"
@@ -40,46 +39,6 @@ func promptForGit() (gitconf config.GitCfg) {
4039
return
4140
}
4241

43-
func parseWebstring(webstring string) (webconf config.WebCfg) {
44-
errmsg := fmt.Sprintf("invalid web configuration line %s", webstring)
45-
split := strings.SplitN(webstring, "://", 2)
46-
if len(split) != 2 {
47-
Die(errmsg)
48-
}
49-
webconf.Protocol = split[0]
50-
51-
split = strings.SplitN(split[1], ":", 2)
52-
if len(split) != 2 {
53-
Die(errmsg)
54-
}
55-
port, err := strconv.ParseUint(split[1], 10, 16)
56-
if err != nil {
57-
Die(fmt.Sprintf("%s: %s", errmsg, ginerrors.BadPort))
58-
}
59-
webconf.Host, webconf.Port = split[0], uint16(port)
60-
return
61-
}
62-
63-
func parseGitstring(gitstring string) (gitconf config.GitCfg) {
64-
errmsg := fmt.Sprintf("invalid git configuration line %s", gitstring)
65-
split := strings.SplitN(gitstring, "@", 2)
66-
if len(split) != 2 {
67-
Die(errmsg)
68-
}
69-
gitconf.User = split[0]
70-
71-
split = strings.SplitN(split[1], ":", 2)
72-
if len(split) != 2 {
73-
Die(errmsg)
74-
}
75-
port, err := strconv.ParseUint(split[1], 10, 16)
76-
if err != nil {
77-
Die(fmt.Sprintf("%s: %s", errmsg, ginerrors.BadPort))
78-
}
79-
gitconf.Host, gitconf.Port = split[0], uint16(port)
80-
return
81-
}
82-
8342
func addHostKey(gitconf *config.GitCfg) {
8443
hostkeystr, fingerprint, err := git.GetHostKey(*gitconf)
8544
CheckError(err)
@@ -115,22 +74,25 @@ func addServer(cmd *cobra.Command, args []string) {
11574

11675
serverConf := config.ServerCfg{}
11776

77+
var err error
11878
if webstring == "" {
11979
serverConf.Web = promptForWeb()
12080
} else {
121-
serverConf.Web = parseWebstring(webstring)
81+
serverConf.Web, err = config.ParseWebString(webstring)
82+
CheckError(err)
12283
}
12384

12485
if gitstring == "" {
12586
serverConf.Git = promptForGit()
12687
} else {
127-
serverConf.Git = parseGitstring(gitstring)
88+
serverConf.Git, err = config.ParseGitString(gitstring)
89+
CheckError(err)
12890
}
12991

13092
addHostKey(&serverConf.Git)
13193

13294
// Save to config
133-
err := config.AddServerConf(alias, serverConf)
95+
err = config.AddServerConf(alias, serverConf)
13496
if err != nil {
13597
Die("failed to update configuration file")
13698
}

gincmd/ginerrors/errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@ const (
99

1010
// BadPort is returned when a port number is outside the valid range (1..65535)
1111
BadPort = "port must be between 0 and 65535 (inclusive)"
12+
13+
// MissingURLScheme is returned when a string is assumed to be a URL but does not contain a scheme (missing ://)
14+
MissingURLScheme = "could not determine protocol scheme (no ://)"
15+
16+
// MissingGitUser is returned when a string is assumed to be a git configuration but does not contain a user (missing @)
17+
MissingGitUser = "could not determine git username (no @)"
1218
)

0 commit comments

Comments
 (0)