commands

package
v0.0.0-...-a3a871f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2016 License: LGPL-2.1 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AboutCmd = &cobra.Command{
	Use:   "about",
	Short: "Version and authorship information",
	Long:  `Version and authorship information`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("latch-cmd - version 1.0 - Mikel Pintor (millen@gmail.com) - more info at: https://github.com/millenc/latch-cmd")
	},
}

About command. Outputs version and authorship information.

View Source
var AccountID string

Flag variables

View Source
var AppCmd = &cobra.Command{
	Use:   "app",
	Short: "Set of commands to interact with the main application API.",
	Long:  `Set of commands to interact with the main application API.`,
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Help()
	},
	PersistentPreRun: func(cmd *cobra.Command, args []string) {

		if cmd.Use != "app" && cmd.Use != "operation" {
			if l, err := util.NewLatch(viper.GetString("app"), viper.GetString("secret"), viper.GetString("proxy"), Verbose, util.ShowRequestInfoFn(Session, NoShadow), util.ShowResponseInfoFn(Session)); err != nil {
				Session.Halt(err)
			} else {
				Latch = l
			}
		}
	},
	PersistentPostRun: func(cmd *cobra.Command, args []string) {
		Session.End()
	},
}
View Source
var AppID string

Flag variables

View Source
var Application string
View Source
var ApplicationAddCmd = &cobra.Command{
	Use:   "add",
	Short: "Adds a new application",
	Run: func(cmd *cobra.Command, args []string) {
		if Name == "" {
			Session.Halt(errors.New("You must provide the name of the new application (--name)."))
		}

		applicationInfo := &golatch.LatchApplicationInfo{
			Name:          Name,
			ContactEmail:  ContactEmail,
			ContactPhone:  ContactPhone,
			TwoFactor:     TwoFactor,
			LockOnRequest: LockOnRequest,
		}

		if resp, err := LatchUser.AddApplication(applicationInfo); err == nil {
			if Bare {
				Session.OutputAndExit(fmt.Sprintf("%s:%s", resp.AppID(), resp.Secret()))
			} else {
				Session.AddSuccess("application succesfully created!:\t")
				Session.AddInfo("app id\t" + resp.AppID())
				Session.AddInfo("secret key\t" + util.FormatSecret(resp.Secret(), NoShadow))
			}
		} else {
			Session.Halt(err)
		}
	},
}

Add application command

View Source
var ApplicationCmd = &cobra.Command{
	Use:   "application",
	Short: "Manages Latch applications",
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Help()
	},
}

Application base command

View Source
var ApplicationDeleteCmd = &cobra.Command{
	Use:   "delete",
	Short: "Deletes an existing application",
	Run: func(cmd *cobra.Command, args []string) {
		if Application == "" {
			Session.Halt(errors.New("You must provide the ID of the application that you want to delete (--app)."))
		}

		if err := LatchUser.DeleteApplication(Application); err == nil {
			Session.AddSuccess("application succesfully deleted!")
		} else {
			Session.Halt(err)
		}
	},
}

Delete operation command

View Source
var ApplicationShowCmd = &cobra.Command{
	Use:   "show",
	Short: "Shows information about your applications",
	Run: func(cmd *cobra.Command, args []string) {
		if resp, err := LatchUser.ShowApplications(); err == nil {
			var i int = 1
			for applicationId, applicationInfo := range resp.Applications() {
				Session.AddInfo(fmt.Sprintf("application #%d:", i))
				Session.AddInfo("name\t" + applicationInfo.Name)
				Session.AddInfo("id\t" + applicationId)
				Session.AddInfo("secret key\t" + util.FormatSecret(applicationInfo.Secret, NoShadow))
				Session.AddInfo("two factor\t" + applicationInfo.TwoFactor)
				Session.AddInfo("lock on request\t" + applicationInfo.LockOnRequest)
				Session.AddInfo("phone\t" + applicationInfo.ContactPhone)
				Session.AddInfo("email\t" + applicationInfo.ContactEmail)
				Session.AddInfo("image\t" + applicationInfo.ImageURL + "\n")

				i++
			}
		} else {
			Session.Halt(err)
		}
	},
}

Show applications command

View Source
var ApplicationUpdateCmd = &cobra.Command{
	Use:   "update",
	Short: "Updates an existing application",
	Run: func(cmd *cobra.Command, args []string) {
		if Application == "" {
			Session.Halt(errors.New("You must provide the ID of the application that you want to update (--app)."))
		}

		applicationInfo := &golatch.LatchApplicationInfo{
			Name:          Name,
			ContactEmail:  ContactEmail,
			ContactPhone:  ContactPhone,
			TwoFactor:     TwoFactor,
			LockOnRequest: LockOnRequest,
		}

		if err := LatchUser.UpdateApplication(Application, applicationInfo); err == nil {
			Session.AddSuccess("application succesfully updated!")
		} else {
			Session.Halt(err)
		}
	},
}

Update operation command

View Source
var Bare bool
View Source
var ContactEmail string

Flag variables

View Source
var ContactPhone string
View Source
var From string

Flag variables

View Source
var HistoryCmd = &cobra.Command{
	Use:   "history",
	Short: "Gets history information about an account. You can filter events between the --from and --to dates.",
	Run: func(cmd *cobra.Command, args []string) {
		var FromDate, ToDate t.Time
		var err error

		if AccountID == "" {
			Session.Halt(errors.New("You must provide an Account ID (--account)."))
		}

		if FromDate, err = util.ParseCmdDate(From); err != nil {
			Session.Halt(errors.New("The 'from' date has an incorrect format (please use dd-mm-yyyy hh:ii:ss)"))
		}
		if ToDate, err = util.ParseCmdDate(To); err != nil {
			Session.Halt(errors.New("The 'to' date has an incorrect format (please use dd-mm-yyyy hh:ii:ss)"))
		}

		if resp, err := Latch.History(AccountID, FromDate, ToDate); err == nil {

			buffer := &bytes.Buffer{}
			table := tablewriter.NewWriter(buffer)
			table.SetHeader([]string{"Time", "Action", "What", "Was", "Value", "Name", "User Agent", "IP"})
			for _, entry := range resp.History() {
				time := t.Unix(0, entry.Time*1000000)
				table.Append([]string{time.Format("02-01-2006 15:04:05"), entry.Action, entry.What, entry.Was, entry.Value, entry.Name, entry.UserAgent, entry.IP})
			}
			table.Render()

			//Generate output
			var output string
			output += "Last seen: " + t.Unix(0, resp.LastSeen()*1000000).Format("02-01-2006 15:04:05") + ", "
			output += "Client version: [" + util.FormatClientVersions(resp.ClientVersion()) + "], "
			output += "History count: " + fmt.Sprintf("%d", resp.HistoryCount()) + "\n\n"
			output += buffer.String()
			Session.AddSuccess(output)
		} else {
			Session.Halt(err)
		}
	},
}

Status command

Latch struct

View Source
var LatchUser *golatch.LatchUser

Latch struct

View Source
var LockCmd = &cobra.Command{
	Use:   "lock",
	Short: "Locks an account using it's account ID (--account).",
	Run: func(cmd *cobra.Command, args []string) {
		if AccountID == "" {
			Session.Halt(errors.New("You must provide an Account ID (--account)."))
		}

		if err := Latch.Lock(AccountID); err == nil {
			Session.AddSuccess("account locked!")
		} else {
			Session.Halt(err)
		}
	},
}

Lock command

View Source
var LockOnRequest string
View Source
var MainCmd = &cobra.Command{
	Use:   "latch-cmd",
	Short: "Latch-cmd is an unofficial command line tool that lets you interact with the Latch API (https://latch.elevenpaths.com/).",
	Long:  `Latch-cmd is an unofficial command line tool that lets you interact with the Latch API (https://latch.elevenpaths.com/).`,
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Help()
	},
}

Main command. Outputs help.

View Source
var Name string
View Source
var NoOTP bool
View Source
var NoShadow bool
View Source
var OperationAddCmd = &cobra.Command{
	Use:   "add",
	Short: "Adds a new operation",
	Run: func(cmd *cobra.Command, args []string) {
		if ParentID == "" {
			Session.Halt(errors.New("You must provide the ID of the parent operation or application (--parent)."))
		}
		if Name == "" {
			Session.Halt(errors.New("You must provide the new operation's name (--name)."))
		}

		if resp, err := Latch.AddOperation(ParentID, Name, TwoFactor, LockOnRequest); err == nil {
			if Bare {
				Session.OutputAndExit(resp.OperationId())
			} else {
				Session.AddSuccess("operation created with ID: " + resp.OperationId())
			}
		} else {
			Session.Halt(err)
		}
	},
}

Add operation command

View Source
var OperationCmd = &cobra.Command{
	Use:   "operation",
	Short: "Manages Latch operations",
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Help()
	},
}
View Source
var OperationDeleteCmd = &cobra.Command{
	Use:   "delete",
	Short: "Deletes an operation",
	Run: func(cmd *cobra.Command, args []string) {
		if OperationID == "" {
			Session.Halt(errors.New("You must provide the operation's ID (--operation)."))
		}

		if err := Latch.DeleteOperation(OperationID); err == nil {
			Session.AddSuccess("operation was deleted!")
		} else {
			Session.Halt(err)
		}
	},
}

Delete operation command

View Source
var OperationID string

Flag variables

View Source
var OperationLockCmd = &cobra.Command{
	Use:   "lock",
	Short: "Locks the operation using it's account ID (--account) and operation ID (--operation).",
	Run: func(cmd *cobra.Command, args []string) {
		if AccountID == "" {
			Session.Halt(errors.New("You must provide an Account ID (--account)."))
		}
		if OperationID == "" {
			Session.Halt(errors.New("You must provide an Operation ID (--operation)."))
		}

		if err := Latch.LockOperation(AccountID, OperationID); err == nil {
			Session.AddSuccess("operation locked!")
		} else {
			Session.Halt(err)
		}
	},
}

Operation lock command

View Source
var OperationShowCmd = &cobra.Command{
	Use:   "show",
	Short: "Shows information about an operation",
	Run: func(cmd *cobra.Command, args []string) {
		if resp, err := Latch.ShowOperation(OperationID); err == nil {
			id, operation := resp.FirstOperation()

			Session.AddSuccess("operation info:\t")
			Session.AddInfo("id\t" + id)
			Session.AddInfo("name\t" + operation.Name)
			Session.AddInfo("two factor\t" + operation.TwoFactor)
			Session.AddInfo("lock on request\t" + operation.LockOnRequest)
		} else {
			Session.Halt(err)
		}
	},
}

Show operation command

View Source
var OperationStatusCmd = &cobra.Command{
	Use:   "status",
	Short: "Gets the current status of an operation using an account ID (--account) and an operation ID (--operation).",
	Run: func(cmd *cobra.Command, args []string) {
		if AccountID == "" {
			Session.Halt(errors.New("You must provide an Account ID (--account)."))
		}
		if OperationID == "" {
			Session.Halt(errors.New("You must provide an Operation ID (--operation)."))
		}

		if resp, err := Latch.OperationStatus(AccountID, OperationID, NoOTP, Silent); err == nil {

			if resp.Status() != golatch.LATCH_STATUS_ON {
				Session.ExitCode = 1
			}

			//Output
			var bareOutput string
			if Bare {
				bareOutput += resp.Status()
			} else {
				Session.AddSuccess("operation is " + resp.Status())
			}

			TwoFactor := resp.TwoFactor()
			if TwoFactor.Token != "" {
				if Bare {
					bareOutput += fmt.Sprintf(":%s:%d", TwoFactor.Token, TwoFactor.Generated)
				} else {
					Session.AddInfo("two factor info:\t")
					Session.AddInfo("token\t" + TwoFactor.Token)
					Session.AddInfo(fmt.Sprintf("generated\t%d (%s)", TwoFactor.Generated, time.Unix(TwoFactor.Generated/1000, 0)))
				}
			}

			if Bare {
				Session.OutputAndExit(bareOutput)
			}
		} else {
			Session.Halt(err)
		}
	},
}

Status command

View Source
var OperationUnlockCmd = &cobra.Command{
	Use:   "unlock",
	Short: "Unlocks the operation using it's account ID (--account) and operation ID (--operation).",
	Run: func(cmd *cobra.Command, args []string) {
		if AccountID == "" {
			Session.Halt(errors.New("You must provide an Account ID (--account)."))
		}
		if OperationID == "" {
			Session.Halt(errors.New("You must provide an Operation ID (--operation)."))
		}

		if err := Latch.UnlockOperation(AccountID, OperationID); err == nil {
			Session.AddSuccess("operation unlocked!")
		} else {
			Session.Halt(err)
		}
	},
}

Operation unlock command

View Source
var OperationUpdateCmd = &cobra.Command{
	Use:   "update",
	Short: "Updates an operation",
	Run: func(cmd *cobra.Command, args []string) {
		if OperationID == "" {
			Session.Halt(errors.New("You must provide the operation's ID (--operation)."))
		}

		if err := Latch.UpdateOperation(OperationID, Name, TwoFactor, LockOnRequest); err == nil {
			Session.AddSuccess("operation was updated successfully!")
		} else {
			Session.Halt(err)
		}
	},
}

Update operation command

View Source
var PairCmd = &cobra.Command{
	Use:   "pair",
	Short: "Pairs an account with the provided pairing token (--token).",
	Run: func(cmd *cobra.Command, args []string) {
		if Token == "" {
			Session.Halt(errors.New("You must provide the pairing token (--token)."))
		}

		if resp, err := Latch.Pair(Token); err == nil {
			if Bare {
				Session.OutputAndExit(resp.AccountId())
			} else {
				Session.AddSuccess("pairing done! Account ID is " + resp.AccountId())
			}
		} else {
			Session.Halt(err)
		}
	},
}

Pair command

View Source
var ParentID string

Flag variables

View Source
var Proxy string
View Source
var SecretKey string

Command session

View Source
var Silent bool
View Source
var StatusCmd = &cobra.Command{
	Use:   "status",
	Short: "Gets the current status of an account using it's account ID (--account).",
	Run: func(cmd *cobra.Command, args []string) {
		if AccountID == "" {
			Session.Halt(errors.New("You must provide an Account ID (--account)."))
		}

		if resp, err := Latch.Status(AccountID, NoOTP, Silent); err == nil {

			if resp.Status() != golatch.LATCH_STATUS_ON {
				Session.ExitCode = 1
			}

			//Output
			var bareOutput string
			if Bare {
				bareOutput += resp.Status()
			} else {
				Session.AddSuccess("account is " + resp.Status())
			}

			TwoFactor := resp.TwoFactor()
			if TwoFactor.Token != "" {
				if Bare {
					bareOutput += fmt.Sprintf(":%s:%d", TwoFactor.Token, TwoFactor.Generated)
				} else {
					Session.AddInfo("two factor info:\t")
					Session.AddInfo("token\t" + TwoFactor.Token)
					Session.AddInfo(fmt.Sprintf("generated\t%d (%s)", TwoFactor.Generated, time.Unix(TwoFactor.Generated/1000, 0)))
				}
			}

			if Bare {
				Session.OutputAndExit(bareOutput)
			}
		} else {
			Session.Halt(err)
		}
	},
}

Status command

View Source
var SubscriptionCmd = &cobra.Command{
	Use:   "subscription",
	Short: "Gets information about your current subscription.",
	Run: func(cmd *cobra.Command, args []string) {
		if resp, err := LatchUser.Subscription(); err == nil {
			Session.AddInfo("subscription info:\t")
			Session.AddInfo("id\t" + resp.ID())
			Session.AddInfo("applications\t" + util.FormatUsageString(resp.Applications().InUse, resp.Applications().Limit))
			Session.AddInfo("users\t" + util.FormatUsageString(resp.Users().InUse, resp.Users().Limit))
			Session.AddInfo("operations:\t")
			for name, usage := range resp.Operations() {
				Session.AddInfo(name + "\t" + util.FormatUsageString(usage.InUse, usage.Limit))
			}
		} else {
			Session.Halt(err)
		}
	},
}

Subscription command

View Source
var Token string

Flag variables

View Source
var TwoFactor string
View Source
var UnlockCmd = &cobra.Command{
	Use:   "unlock",
	Short: "Unlocks an account using it's account ID (--account).",
	Run: func(cmd *cobra.Command, args []string) {
		if AccountID == "" {
			Session.Halt(errors.New("You must provide an Account ID (--account)."))
		}

		if err := Latch.Unlock(AccountID); err == nil {
			Session.AddSuccess("account unlocked!")
		} else {
			Session.Halt(err)
		}
	},
}

Unlock command

View Source
var UnpairCmd = &cobra.Command{
	Use:   "unpair",
	Short: "Unpairs an account using it's account ID (--account).",
	Run: func(cmd *cobra.Command, args []string) {
		if AccountID == "" {
			Session.Halt(errors.New("You must provide an Account ID (--account)."))
		}

		if err := Latch.Unpair(AccountID); err == nil {
			Session.AddSuccess("unpair done!")
		} else {
			Session.Halt(err)
		}
	},
}

Unpair command

View Source
var UserCmd = &cobra.Command{
	Use:   "user",
	Short: "Set of commands to interact with the user API (manage applications and subscription information).",
	Long:  `Set of commands to interact with the user API (manage applications and subscription information).`,
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Help()
	},
	PersistentPreRun: func(cmd *cobra.Command, args []string) {

		if cmd.Use != "user" && cmd.Use != "application" {
			if l, err := util.NewLatchUser(viper.GetString("user"), viper.GetString("user_secret"), viper.GetString("proxy"), Verbose, util.ShowRequestInfoFn(Session, NoShadow), util.ShowResponseInfoFn(Session)); err != nil {
				Session.Halt(err)
			} else {
				LatchUser = l
			}
		}
	},
	PersistentPostRun: func(cmd *cobra.Command, args []string) {
		Session.End()
	},
}
View Source
var UserID string

Flag variables

View Source
var Verbose bool

Functions

func AddCommands

func AddCommands()

Adds subcommands to the root command

func Execute

func Execute()

Adds commands and executes them

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL