/
session.go
100 lines (86 loc) · 2.38 KB
/
session.go
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
package cmd
import (
"strconv"
"strings"
"time"
"github.com/mobingi/mobingi-cli/pkg/cli"
"github.com/mobingi/mobingi-cli/pkg/cli/confmap"
"github.com/mobingilabs/mobingi-sdk-go/client"
"github.com/mobingilabs/mobingi-sdk-go/mobingi/session"
d "github.com/mobingilabs/mobingi-sdk-go/pkg/debug"
"github.com/mobingilabs/mobingi-sdk-go/pkg/nativestore"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
func getApiVersionInt() int {
v := 3
vparam := viper.GetString(confmap.ConfigKey("apiver"))
in, err := strconv.Atoi(strings.TrimLeft(vparam, "v"))
if err != nil {
return -1
}
v = in
return v
}
func clisession() (*session.Session, error) {
verbose := viper.GetBool(confmap.ConfigKey("verbose"))
dbg := viper.GetBool(confmap.ConfigKey("debug"))
v := getApiVersionInt()
if v < 0 {
return nil, errors.New("cannot get api version")
}
// check if we have credentials in nativestore
user, secret, err := nativestore.Get(cli.CliLabel, cli.CliUrl)
if err == nil {
cred1 := strings.Split(user, "|")
cred2 := strings.Split(secret, "|")
var id, sec, u, p string
id = cred1[0]
sec = cred2[0]
if len(cred1) == 2 {
u = cred1[1]
}
if len(cred2) == 2 {
p = cred2[1]
}
if cred1[0] != "" && cred2[0] != "" {
if verbose {
str := "use credentials from native store: " + id
if u != "" {
str += "|" + u
}
d.Info(str)
}
return session.New(&session.Config{
ClientId: id,
ClientSecret: sec,
Username: u,
Password: p,
ApiVersion: v,
BaseApiUrl: viper.GetString(confmap.ConfigKey("url")),
BaseRegistryUrl: viper.GetString(confmap.ConfigKey("rurl")),
Sesha3Url: viper.GetString(confmap.ConfigKey("surl")),
HttpClientConfig: &client.Config{
Timeout: time.Second * time.Duration(viper.GetInt64(confmap.ConfigKey("timeout"))),
Verbose: verbose,
},
})
}
}
if verbose {
d.Info("cannot access native store, use config file token")
if dbg {
d.ErrorD(err)
}
}
return session.New(&session.Config{
ApiVersion: v,
AccessToken: viper.GetString(confmap.ConfigKey("token")),
BaseApiUrl: viper.GetString(confmap.ConfigKey("url")),
BaseRegistryUrl: viper.GetString(confmap.ConfigKey("rurl")),
HttpClientConfig: &client.Config{
Timeout: time.Second * time.Duration(viper.GetInt64(confmap.ConfigKey("timeout"))),
Verbose: verbose,
},
})
}