--- a/cmd/gondole-cli/main.go Wed Apr 12 14:20:47 2017 +0200
+++ b/cmd/gondole-cli/main.go Wed Apr 12 14:22:04 2017 +0200
@@ -5,17 +5,95 @@
"github.com/urfave/cli"
"log"
"os"
+ "strings"
)
var (
- fVerbose bool
- fBaseURL string
- instance *gondole.Gondole
- cnf *gondole.Config
+ fVerbose bool
+ fInstance string
+ fScopes string
+
+ instance *gondole.Client
+ cnf *Server
+
+ // For bootstrapping, override the API endpoint w/o any possible /api/vN, that is
+ // supplied by the library
+ APIEndpoint string
+
+ // Deduced though the full instance URL when registering
+ InstanceName string
+
+ // Default scopes
+ ourScopes = []string{
+ "read",
+ "write",
+ "follow",
+ }
)
-func Register(c *cli.Context) (err error) {
- instance, err = gondole.NewApp("gondole-cli", nil, gondole.NoRedirect, fBaseURL)
+// Config holds our parameters
+type Server struct {
+ ID string `json:"id"`
+ Name string `json:"name"`
+ BearerToken string `json:"bearer_token"`
+ BaseURL string `json:"base_url"` // Allow for overriding APIEndpoint on registration
+}
+
+type Config struct {
+ Default string
+}
+
+func setupEnvironment(c *cli.Context) (err error) {
+ var scopes []string
+
+ if fInstance != "" {
+ InstanceName = basename(fInstance)
+ APIEndpoint = filterURL(fInstance)
+ }
+
+ // Load configuration, will register if none is found
+ cnf, err = LoadConfig(InstanceName)
+ if err != nil {
+ // Nothing exist yet
+ defName := Config{
+ Default: InstanceName,
+ }
+ err = defName.Write()
+ if err != nil {
+ log.Fatalf("error: can not write config for %s", InstanceName)
+ }
+
+ // Now register this through OAuth
+ if fScopes != "" {
+ scopes = strings.Split(fScopes, " ")
+ } else {
+ scopes = ourScopes
+ }
+
+ instance, err = gondole.NewApp("gondole-cli", scopes, gondole.NoRedirect, fInstance)
+
+ server := &Server{
+ ID: instance.ID,
+ Name: instance.Name,
+ BearerToken: instance.Secret,
+ BaseURL: instance.APIBase,
+ }
+ err = server.WriteToken(InstanceName)
+ if err != nil {
+ log.Fatalf("error: can not write token for %s", instance.Name)
+ }
+
+ cnf := Config{
+ Default: instance.Name,
+ }
+
+ err = cnf.Write()
+ if err != nil {
+ log.Fatalf("error: can not write config for %s", instance.Name)
+ }
+
+ }
+ // Log in to the instance
return err
}
@@ -36,7 +114,7 @@
app.Version = gondole.APIVersion
//app.HideVersion = true
- app.Before = Register
+ app.Before = setupEnvironment
app.Flags = []cli.Flag{
cli.BoolFlag{
@@ -45,9 +123,14 @@
Destination: &fVerbose,
},
cli.StringFlag{
- Name: "instance,i",
+ Name: "instance,I",
Usage: "use that instance",
- Destination: &fBaseURL,
+ Destination: &fInstance,
+ },
+ cli.StringFlag{
+ Name: "scopes,S",
+ Usage: "use these scopes",
+ Destination: &fScopes,
},
}
app.Run(os.Args)