cmd/gondole-cli/main.go
changeset 83 adc39ae774c0
parent 76 dd9b9c02bfff
child 84 519be52bfced
equal deleted inserted replaced
81:ba90955d2d56 83:adc39ae774c0
    14 	fScopes   string
    14 	fScopes   string
    15 
    15 
    16 	instance *gondole.Client
    16 	instance *gondole.Client
    17 	cnf      *Server
    17 	cnf      *Server
    18 
    18 
    19 	// For bootstrapping, override the API endpoint w/o any possible /api/vN, that is
       
    20 	// supplied by the library
       
    21 	APIEndpoint string
       
    22 
       
    23 	// Deduced though the full instance URL when registering
       
    24 	InstanceName string
       
    25 
       
    26 	// Default scopes
    19 	// Default scopes
    27 	ourScopes = []string{
    20 	ourScopes = []string{
    28 		"read",
    21 		"read",
    29 		"write",
    22 		"write",
    30 		"follow",
    23 		"follow",
    31 	}
    24 	}
       
    25 
       
    26 	defaultInstanceURL = "https://mastodon.social"
    32 )
    27 )
    33 
    28 
    34 // Config holds our parameters
    29 // Config holds our parameters
    35 type Server struct {
    30 type Server struct {
    36 	ID          string `json:"id"`
    31 	ID          string `json:"id"`
    37 	Name        string `json:"name"`
    32 	Name        string `json:"name"`
    38 	BearerToken string `json:"bearer_token"`
    33 	BearerToken string `json:"bearer_token"`
    39 	BaseURL     string `json:"base_url"` // Allow for overriding APIEndpoint on registration
    34 	APIBase     string `json:"base_url"`
       
    35 	InstanceURL string `json:"base_url"`
    40 }
    36 }
    41 
    37 
    42 type Config struct {
    38 type Config struct {
    43 	Default string
    39 	Default string
    44 }
    40 }
    45 
    41 
    46 func setupEnvironment(c *cli.Context) (err error) {
    42 func setupEnvironment(c *cli.Context) (err error) {
    47 	var scopes []string
    43 	var scopes []string
    48 
    44 
       
    45 	instanceURL := defaultInstanceURL
    49 	if fInstance != "" {
    46 	if fInstance != "" {
    50 		InstanceName = basename(fInstance)
    47 		if strings.Contains(fInstance, "://") {
    51 		APIEndpoint = filterURL(fInstance)
    48 			instanceURL = fInstance
       
    49 		} else {
       
    50 			instanceURL = "https://" + fInstance
       
    51 		}
    52 	}
    52 	}
    53 
    53 
       
    54 	instanceName := basename(instanceURL)
       
    55 
    54 	// Load configuration, will register if none is found
    56 	// Load configuration, will register if none is found
    55 	cnf, err = LoadConfig(InstanceName)
    57 	cnf, err = LoadConfig(instanceName)
    56 	if err != nil {
    58 	if err != nil {
    57 		// Nothing exist yet
    59 		// Nothing exist yet
    58 		defName := Config{
    60 		defName := Config{
    59 			Default: InstanceName,
    61 			Default: instanceName,
    60 		}
    62 		}
    61 		err = defName.Write()
    63 		err = defName.Write()
    62 		if err != nil {
    64 		if err != nil {
    63 			log.Fatalf("error: can not write config for %s", InstanceName)
    65 			log.Fatalf("error: can not write config for %s", instanceName)
    64 		}
    66 		}
    65 
    67 
    66 		// Now register this through OAuth
    68 		// Now register this through OAuth
    67 		if fScopes != "" {
    69 		if fScopes != "" {
    68 			scopes = strings.Split(fScopes, " ")
    70 			scopes = strings.Split(fScopes, " ")
    69 		} else {
    71 		} else {
    70 			scopes = ourScopes
    72 			scopes = ourScopes
    71 		}
    73 		}
    72 
    74 
    73 		instance, err = gondole.NewApp("gondole-cli", scopes, gondole.NoRedirect, fInstance)
    75 		instance, err = gondole.NewApp("gondole-cli", scopes, gondole.NoRedirect, instanceURL)
    74 
    76 
    75 		server := &Server{
    77 		server := &Server{
    76 			ID:          instance.ID,
    78 			ID:          instance.ID,
    77 			Name:        instance.Name,
    79 			Name:        instance.Name,
    78 			BearerToken: instance.Secret,
    80 			BearerToken: instance.Secret,
    79 			BaseURL:     instance.APIBase,
    81 			APIBase:     instance.APIBase,
       
    82 			InstanceURL: instance.InstanceURL,
    80 		}
    83 		}
    81 		err = server.WriteToken(InstanceName)
    84 		err = server.WriteToken(instanceName)
    82 		if err != nil {
    85 		if err != nil {
    83 			log.Fatalf("error: can not write token for %s", instance.Name)
    86 			log.Fatalf("error: can not write token for %s", instance.Name)
    84 		}
    87 		}
    85 
    88 
    86 		cnf := Config{
    89 		cnf := Config{
    99 
   102 
   100 func init() {
   103 func init() {
   101 	cli.VersionFlag = cli.BoolFlag{Name: "version, V"}
   104 	cli.VersionFlag = cli.BoolFlag{Name: "version, V"}
   102 
   105 
   103 	cli.VersionPrinter = func(c *cli.Context) {
   106 	cli.VersionPrinter = func(c *cli.Context) {
   104 		log.Printf("API wrapper: %s Mastodon CLI: %s\n", c.App.Version, gondole.APIVersion)
   107 		log.Printf("API wrapper: %s Mastodon CLI: %s\n", c.App.Version, gondole.GondoleVersion)
   105 	}
   108 	}
   106 }
   109 }
   107 
   110 
   108 func main() {
   111 func main() {
   109 
   112 
   110 	app := cli.NewApp()
   113 	app := cli.NewApp()
   111 	app.Name = "gondole"
   114 	app.Name = "gondole"
   112 	app.Usage = "Mastodon CLI interface"
   115 	app.Usage = "Mastodon CLI interface"
   113 	app.Author = "Ollivier Robert <roberto@keltia.net>"
   116 	app.Author = "Ollivier Robert <roberto@keltia.net>"
   114 	app.Version = gondole.APIVersion
   117 	app.Version = gondole.GondoleVersion
   115 	//app.HideVersion = true
   118 	//app.HideVersion = true
   116 
   119 
   117 	app.Before = setupEnvironment
   120 	app.Before = setupEnvironment
   118 
   121 
   119 	app.Flags = []cli.Flag{
   122 	app.Flags = []cli.Flag{