cli

package module
v0.0.0-...-f49da63 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 8 Imported by: 3

README

singlecli

singlecli - package for creating single command console application

App definition

It's simple app for adding numbers

package main

import (
	"context"
	"fmt"
	"strconv"
	"time"

	"github.com/artarts36/singlecli"
)

func main() {
	application := cli.App{
		BuildInfo: &cli.BuildInfo{
			Name:      "number-adder",
			Version:   "0.1.0",
			BuildDate: time.Now().String(),
		},
		Action: run,
		Args: []*cli.ArgDefinition{
			{
				Name:        "num1",
				Description: "first number",
				Required:    true,
			},
			{
				Name:        "num2",
				Description: "second number",
				Required:    true,
			},
		},
		UsageExamples: []*cli.UsageExample{
			{
				Command: "number-adder 1 and 2",
			},
		},
	}

	application.RunWithGlobalArgs(context.Background())
}

func run(ctx *cli.Context) error {
	number1, _ := strconv.Atoi(ctx.GetArg("number1"))
	number2, _ := strconv.Atoi(ctx.GetArg("number2"))

	fmt.Printf("%d + %d = %d", number1, number2, number1 + number2)

	return nil
}

Additional features

Generate GitHub Action configuration

Run: go run you_main_file.go --singlecli-codegen-ga

You also can generate in pipeline with workflow:

name: update github action config

permissions: write-all

on:
  push:
    branches:
      - master

jobs:
  update-ga-config:
    runs-on: ubuntu-latest
    steps:
      - name: install deps
        run: sudo apt install gcc

      - name: Check out code
        uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v4 # action page: <https://github.com/actions/setup-go>
        with:
          go-version: stable

      - name: Install Go dependencies
        run: go mod download

      - name: Configure git user
        run: |
          git config user.name 'github-actions[bot]'
          git config user.email 'github-actions[bot]@users.noreply.github.com'

      - name: Hash prev version
        id: prev_version
        run: echo "hash=${{ hashFiles('action.yaml') }}" >> $GITHUB_OUTPUT

      - name: Generate new config file
        run: |
          go run ./cmd/main.go --singlecli-codegen-ga

      - name: Commit changes
        if: ${{ steps.prev_version.outputs.hash != hashFiles('action.yaml') }}
        run: |
          git add action.yaml
          git commit -m "chore: actualize ga config"
          git push

Application examples

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action func(ctx *Context) error

type App

type App struct {
	BuildInfo     *BuildInfo
	Args          []*ArgDefinition
	Opts          []*OptDefinition
	Action        Action
	UsageExamples []*UsageExample
}

func (*App) Run

func (a *App) Run(ctx context.Context, args []string)

func (*App) RunWithGlobalArgs

func (a *App) RunWithGlobalArgs(ctx context.Context)

type ArgDefinition

type ArgDefinition struct {
	Name        string
	Description string
	Required    bool
	ValuesEnum  []string
}

func (*ArgDefinition) HasValuesEnum

func (d *ArgDefinition) HasValuesEnum() bool

func (*ArgDefinition) ValueInputs

func (d *ArgDefinition) ValueInputs(val string) bool

type BuildInfo

type BuildInfo struct {
	Name        string
	Description string
	Version     string
	BuildDate   string
}

type CodegenFile

type CodegenFile struct {
	Name    string
	Content string
}

type Context

type Context struct {
	Context context.Context
	Output  Output

	Args map[string]string
	Opts map[string]string
}

func (*Context) GetArg

func (c *Context) GetArg(name string) string

func (*Context) GetOpt

func (c *Context) GetOpt(name string) (string, bool)

func (*Context) HasOpt

func (c *Context) HasOpt(name string) bool

type OptDefinition

type OptDefinition struct {
	Name        string
	Description string
	WithValue   bool
}

type Output

type Output interface {
	PrintMarkdownTable(headers []string, rows [][]string)
}

type UsageExample

type UsageExample struct {
	Command     string
	Description string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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