clitools

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2025 License: MIT Imports: 17 Imported by: 2

README

CLI tools

The clitools contains helpers for building TT CLI tools. Current functionality includes configuration handling and authenticating against our OIDC provider.

Documentation

Overview

Example
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	"github.com/ttab/clitools"
)

func main() {
	// SampleConf - any addional configuration you want to store.
	type SampleConf struct {
		SomeSetting string `json:"some_setting"`
	}

	env := clitools.EnvStage

	println("Sample application that demonstrates logging in to elephant from a CLI tool\n")

	app, err := clitools.NewConfigurationHandler[SampleConf]("clitools", "elephant-cli")
	if err != nil {
		panic(fmt.Errorf("create configuration handler: %w", err))
	}

	token, err := app.GetAccessToken(context.Background(), env, []string{
		"doc_read",
	})
	if err != nil {
		panic(fmt.Errorf("authenticate: %w", err))
	}

	app.SetConfiguration(SampleConf{
		SomeSetting: "that we want to track",
	})

	err = app.Save()
	if err != nil {
		panic(fmt.Errorf("save configuration: %w", err))
	}

	enc := json.NewEncoder(os.Stdout)

	enc.SetIndent("", "  ")

	println("Current token:")
	_ = enc.Encode(token)
}

Index

Examples

Constants

View Source
const (
	EnvLocal           = "local"
	EnvStage           = "stage"
	EnvProd            = "prod"
	StageOIDCConfigURL = "https://login.stage.tt.se/realms/elephant/.well-known/openid-configuration"
	ProdOIDCConfigURL  = "https://login.tt.se/realms/elephant/.well-known/openid-configuration"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessToken

type AccessToken struct {
	Token         string    `json:"token"`
	Expires       time.Time `json:"expires"`
	Scopes        []string  `json:"scopes"`
	GrantedScopes []string  `json:"granted_scopes"`
}

type ConfigurationHandler

type ConfigurationHandler[T any] struct {
	// contains filtered or unexported fields
}

func NewConfigurationHandler

func NewConfigurationHandler[T any](name string, clientID string) (*ConfigurationHandler[T], error)

func (*ConfigurationHandler[T]) GetAccessToken

func (ac *ConfigurationHandler[T]) GetAccessToken(
	ctx context.Context, environment string, scopes []string,
) (_ AccessToken, outErr error)

func (*ConfigurationHandler[T]) GetConfiguration

func (ac *ConfigurationHandler[T]) GetConfiguration() T

func (*ConfigurationHandler[T]) RegisterEnvironment

func (ac *ConfigurationHandler[T]) RegisterEnvironment(
	ctx context.Context,
	name string, configURL string,
) error

func (*ConfigurationHandler[T]) Save

func (ac *ConfigurationHandler[T]) Save() error

func (*ConfigurationHandler[T]) SetConfiguration

func (ac *ConfigurationHandler[T]) SetConfiguration(conf T)

Jump to

Keyboard shortcuts

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