cmd/madon.go
changeset 45 b58a7ea1aeb2
parent 44 6da40ca4534c
child 46 13717ea83333
equal deleted inserted replaced
44:6da40ca4534c 45:b58a7ea1aeb2
     4 // Please see the LICENSE file is this directory.
     4 // Please see the LICENSE file is this directory.
     5 
     5 
     6 package cmd
     6 package cmd
     7 
     7 
     8 import (
     8 import (
     9 	"errors"
       
    10 	"fmt"
       
    11 	"strconv"
     9 	"strconv"
    12 	"strings"
    10 	"strings"
    13 
    11 
    14 	"github.com/McKael/madon"
    12 	"github.com/McKael/madon"
       
    13 	"github.com/pkg/errors"
    15 	"github.com/spf13/viper"
    14 	"github.com/spf13/viper"
    16 )
    15 )
    17 
    16 
    18 var scopes = []string{"read", "write", "follow"}
    17 var scopes = []string{"read", "write", "follow"}
    19 
    18 
    50 		if err != nil {
    49 		if err != nil {
    51 			return err
    50 			return err
    52 		}
    51 		}
    53 		// Check instance
    52 		// Check instance
    54 		if _, err := gClient.GetCurrentInstance(); err != nil {
    53 		if _, err := gClient.GetCurrentInstance(); err != nil {
    55 			return fmt.Errorf("could not use provided app secrets")
    54 			return errors.New("could not use provided app secrets")
    56 		}
    55 		}
    57 		if verbose {
    56 		if verbose {
    58 			errPrint("Using provided app secrets")
    57 			errPrint("Using provided app secrets")
    59 		}
    58 		}
    60 		return nil
    59 		return nil
    64 		errPrint("Warning: provided app id/secrets incomplete -- registering again")
    63 		errPrint("Warning: provided app id/secrets incomplete -- registering again")
    65 	}
    64 	}
    66 
    65 
    67 	gClient, err = madon.NewApp(AppName, scopes, madon.NoRedirect, instanceURL)
    66 	gClient, err = madon.NewApp(AppName, scopes, madon.NoRedirect, instanceURL)
    68 	if err != nil {
    67 	if err != nil {
    69 		return fmt.Errorf("app registration failed: %s", err.Error())
    68 		return errors.Wrap(err, "app registration failed")
    70 	}
    69 	}
    71 
    70 
    72 	errPrint("Registred new application.")
    71 	errPrint("Registred new application.")
    73 	return nil
    72 	return nil
    74 }
    73 }
    93 	err := gClient.LoginBasic(login, password, scopes)
    92 	err := gClient.LoginBasic(login, password, scopes)
    94 	if err == nil {
    93 	if err == nil {
    95 		return nil
    94 		return nil
    96 	}
    95 	}
    97 	if !verbose && err.Error() == "cannot unmarshal server response: invalid character '<' looking for beginning of value" {
    96 	if !verbose && err.Error() == "cannot unmarshal server response: invalid character '<' looking for beginning of value" {
    98 		return fmt.Errorf("login failed (server did not return a JSON response - check your credentials)")
    97 		return errors.New("login failed (server did not return a JSON response - check your credentials)")
    99 	}
    98 	}
   100 	return fmt.Errorf("login failed: %s", err.Error())
    99 	return errors.Wrap(err, "login failed")
   101 }
   100 }
   102 
   101 
   103 // splitIDs splits a list of IDs into an int64 array
   102 // splitIDs splits a list of IDs into an int64 array
   104 func splitIDs(ids string) (list []int64, err error) {
   103 func splitIDs(ids string) (list []int64, err error) {
   105 	var i int64
   104 	var i int64