cmd

package
v0.0.0-...-09a06b6 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2020 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Copyright © 2020 Matt Gowie <matt@masterpoint.io>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

This section is empty.

Variables

View Source
var (
	// APIKey is the FA API Key created in the FA UI under "Settings > API Keys"
	APIKey string

	// ClientID is the OAuth client_id of our FA Application
	ClientID string

	// TenantID is the ID of our FA instance's default Tenant
	TenantID string
)
View Source
var FetchCmd = &cobra.Command{
	Use:   "fetch",
	Short: "Fetch a random gif from giphy. User must be logged in before using fetch.",
	Run: func(cmd *cobra.Command, args []string) {
		var user fusionauth.User = fusionauth.User{}
		err := Load("/tmp/getgif.json", &user)
		if err != nil && !os.IsNotExist(err) {
			log.Fatal(err)
		}

		if user.Id == "" {
			red := color.New(color.FgRed, color.Bold)
			red.Printf("You're not logged in yet! Please use `getgif login` before using `fetch`.\n")
		} else {
			cyan := color.New(color.FgCyan)
			cyan.Printf("Hey %s, looks like you already logged in so fetching your gif now...\n", user.Email)

			gifURL, err := fetchGif()
			if err != nil {
				log.Fatal(err)
			}

			time.Sleep(2 * time.Second)

			open.Run(gifURL)
		}
	},
}

FetchCmd provides the Cobra sub command for fetching a gif. Requires the user to be logged in via `login`.

View Source
var LoginCmd = &cobra.Command{
	Use:   "login",
	Short: "Authenticate to the FA server using the OAuth Device Flow.",
	Run: func(cmd *cobra.Command, args []string) {
		faClient = fusionauth.NewClient(httpClient, baseURL, APIKey)
		openIDConfig, err := faClient.RetrieveOpenIdConfiguration()
		if err != nil {
			log.Fatal(err)
		}

		deviceResp, err := startDeviceGrantFlow(openIDConfig.DeviceAuthorizationEndpoint)
		if err != nil {
			log.Fatal(err)
		}

		informUserAndOpenBrowser(deviceResp.UserCode)

		accessToken, err := startPolling(openIDConfig.TokenEndpoint, deviceResp.DeviceCode, deviceResp.Interval)
		if err != nil {
			log.Fatal(err)
		}

		fetchAndSaveUser(accessToken)
	},
}

LoginCmd provides the command for logging into the FA server using the Device Flow.

View Source
var LogoutCmd = &cobra.Command{
	Use:   "logout",
	Short: "Logs out the user by removing the user's session from local state.",
	Run: func(cmd *cobra.Command, args []string) {
		Remove("/tmp/getgif.json")

		cyan := color.New(color.FgCyan)
		cyan.Printf("You've been logged out!\n")
	},
}

LogoutCmd logs the user out by cleaning the local state so the user needs to login again.

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

func Load

func Load(path string, v interface{}) error

Load loads the file at the given path into the given struct. Use os.IsNotExist() to see if the returned error is due to the file being missing.

func Remove

func Remove(path string) error

Remove deletes the file at the given path

func Save

func Save(path string, v interface{}) error

Save saves a representation of v to the file at path.

Types

This section is empty.

Jump to

Keyboard shortcuts

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