fleet/cmd/fleetctl/api.go
Mike Arpaia 018e10ea66
Add fleetctl config and auth commands (#1751)
```
$ fleetctl config set address https://localhost:8080
[+] Set the "address" config key to "https://localhost:8080" in the "default" context

$ fleetctl config set ignore_tls true
[+] Set the "ignore_tls" config key to "true" in the "default" context

$ fleetctl setup --email mike@arpaia.co --password "abc123"
[+] Fleet setup successful and context configured!

$ cat ~/.fleet/config
contexts:
  default:
    address: https://localhost:8080
    email: mike@arpaia.co
    ignore_tls: true
    token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6IlUvdm05Vk9wSG0xUlA4SUtjQnBhb2ovWlo1TXppSEVXcFRCNFNPb2tHQnNLUFpDQXFieVpWWnpJb0UvczQzcWkyd1pHZXJOa29SNFVIQ2hNZUc0K09RPT0ifQ.rHawSN8JvD4jjWAPTYX2Ep9ZpMt3u4mSIQcu920C-_s

$ fleetctl logout
[+] Fleet logout successful and local token cleared!

$ cat ~/.fleet/config
contexts:
  default:
    address: https://localhost:8080
    email: mike@arpaia.co
    ignore_tls: true
    token: ""
```
2018-05-04 10:53:21 -06:00

47 lines
1.1 KiB
Go

package main
import (
"fmt"
"github.com/kolide/fleet/server/service"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
func clientFromCLI(c *cli.Context) (*service.Client, error) {
if err := makeConfigIfNotExists(c.String("config")); err != nil {
return nil, errors.Wrapf(err, "error verifying that config exists at %s", c.String("config"))
}
config, err := readConfig(c.String("config"))
if err != nil {
return nil, err
}
cc, ok := config.Contexts[c.String("context")]
if !ok {
return nil, fmt.Errorf("context %q is not found", c.String("context"))
}
if cc.Address == "" {
return nil, errors.New("set the Fleet API address with: fleetctl config set --address https://localhost:8080")
}
fleet, err := service.NewClient(cc.Address, cc.IgnoreTLS)
if err != nil {
return nil, errors.Wrap(err, "error creating Fleet API client handler")
}
t, err := getConfigValue(c, "token")
if err != nil {
return nil, errors.Wrap(err, "error getting token from the config")
}
if token, ok := t.(string); ok {
fleet.SetToken(token)
} else {
return nil, errors.Errorf("token config value was not a string: %+v", t)
}
return fleet, nil
}