internal

package
v0.0.0-...-1ae2f7c Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ModeLog is logging with timestamp
	ModeLog = iota
	// ModePlain is plain printing without a timestamp
	ModePlain
	// ModeNoNewline is printing like plain but without a newline
	ModeNoNewline
)

Variables

View Source
var (
	// Debug - Do not catch panics and prettify them, show debug info
	Debug = flag.Bool("Debug", false, "Enable Debug mode")
	// Params - Set parameters for API method if called
	Params = flag.String("params", "", "Set API method key=value parameters separated by space (format: k=v k=v k=v...)")

	ConfigFilename = flag.String("config", "config.yaml", "Set config file path")
	PrintModeFlag  = flag.Int("output", ModeLog, "Output mode (0 - log with timestamp, 1 - plain log, 2 - no newline)")
	NotInteractive = flag.Bool("no-interactive", false, "Do not ask for any input, use default values")
	NoConfigSave   = flag.Bool("no-save", false, "Do not save the config file on exit (including token)")
	Auth           = flag.String("token", "", "Set auth token for future requests (will be saved in config file; also you can use environment variable KT_CLI_TOKEN)")
	Pretty         = flag.Bool("pretty", false, "Pretty-print JSON responses")
	Passwd         = flag.String("passwd", "", "Set password for encryption/decryption. Also you can use environment variable KT_CLI_PASSWD")
	PublicKeyFile  = flag.String("public", "public_key.pub", "Set public key file path for encryption/decryption (will be downloaded from the server if empty)")
	PrivateKeyFile = flag.String("private", "private_key.asc", "Set private key file path for encryption/decryption (will be downloaded and decrypted from the server if empty)")

	Method = flag.String("act.method", "", "Call API method")
	Ping   = flag.Bool("act.ping", false, "Check if API is alive")

	GetKeys            = flag.String("act.keys", "", "Download keys for the provided disk (\".\" for default disk)")
	GetKeysPublicName  = flag.String("act.keys.public", "public_key.pub", "Set public key name for download")
	GetKeysPrivateName = flag.String("act.keys.private", "private_key.asc", "Set private key name for download")

	Download     = flag.String("act.download", "", "Download file by file ID")
	DownloadPath = flag.String("act.download.path", ".", "Set path to save downloaded file")

	Upload       = flag.String("act.upload", "", "Upload file by path; stdin is also supported")
	UploadName   = flag.String("act.upload.name", "", "Set file name for upload (required for stdin)")
	UploadDisk   = flag.String("act.upload.disk", "", "Set disk for upload")
	UploadFolder = flag.String("act.upload.folder", "", "Set folder for upload")

	FilesList = flag.String("act.files", "", "List files in provided disk")
)

Functions

func ActionApiCall

func ActionApiCall(config *Config)

func ActionAskForToken

func ActionAskForToken(config *Config)

ActionAskForToken asks the user to enter the access token. The token is not displayed on the screen.

func ActionDefault

func ActionDefault(config *Config)

func ActionDownload

func ActionDownload(config *Config)

ActionDownload downloads a file by its ID and saves it to the specified path

func ActionFilesList

func ActionFilesList(config *Config)

func ActionGetKeys

func ActionGetKeys(config *Config)

func ActionPing

func ActionPing()

ActionPing checks if the API is alive and responds to requests

func ActionUpload

func ActionUpload(config *Config, isStdIn bool)

ActionUpload uploads a file to the cloud. The file can be provided by path or by stdin.

func ByteCount

func ByteCount(b int64) string

ByteCount converts bytes to human-readable format

func CheckToken

func CheckToken(token string) (string, error)

CheckToken checks if the token is valid and returns the user id It's a wrapper around GetUserID

func CheckTokenAndAssign

func CheckTokenAndAssign(token string, config *Config) error

CheckTokenAndAssign checks if the token is valid and assigns it to the config. It's a wrapper around CheckToken

func DiskIdOrDefault

func DiskIdOrDefault(config *Config, diskId string) (string, *pkg.Disk, error)

DiskIdOrDefault returns the disk id if it is not empty, otherwise it returns the default disk id It is useful for most users, they usually have only one disk

func GetActualError

func GetActualError(response *pkg.ApiResponse, err error) error

GetActualError returns an error if the response has an error or if there was an error passed as argument. It is useful to avoid checking if the response is nil, and if the response error is nil every time you make a request

func IsStdin

func IsStdin() bool

IsStdin checks if the stdin has data

func JsonToString

func JsonToString(data map[string]interface{}, pretty bool) string

JsonToString converts a map to a string. It makes it easier to print json data. Pretty-prints the json if pretty is true

func NewDefaultCryptoInfo

func NewDefaultCryptoInfo() *pkg.CryptoInfo

func NewProgressBar

func NewProgressBar(max int64) *progressbar.ProgressBar

NewProgressBar @todo integrate with download and upload

func ParseKeyValues

func ParseKeyValues(data string) map[string]interface{}

ParseKeyValues parses a string with key=value pairs separated by spaces and returns a map with the key and value It is used to parse the flags of the command line and other similar cases

func Print

func Print(content string, params ...interface{})

Print prints the content with optional parameters in the way defined by printMode

func PrintError

func PrintError(err string, params ...interface{})

PrintError prints the error message with optional parameters in the way defined by printMode It's a wrapper around fmt.Print that respects printMode

func SaveConfig

func SaveConfig(config *Config, filename string) error

SaveConfig saves the configuration to a YAML file

func ScanEnv

func ScanEnv()

ScanEnv scans environment variables as replacement for the flags that are not set

func SetPrintMode

func SetPrintMode(mode int)

SetPrintMode sets the way of printing messages. See constants like Mode* for available modes This mode is ignored in some cases in interactive mode

Types

type Config

type Config struct {
	UserID string `yaml:"user_id"`
	Token  string `yaml:"token"`
}

Config represents the structure of global configuration

func CreateDefaultConfig

func CreateDefaultConfig() *Config

CreateDefaultConfig creates an empty configuration

func LoadConfig

func LoadConfig(filename string) (*Config, error)

LoadConfig loads the configuration from a YAML file or creates the empty one

Jump to

Keyboard shortcuts

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