gocli

module
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2021 License: CC0-1.0

README

gocli - Minimal Packages for Command-Line Interface

check vulns lint status GitHub license GitHub release

This package is required Go 1.16 or later.

Declare gocli module

See go.mod file.

Usage of gocli package

package main

import (
    "os"

    "github.com/spiegel-im-spiegel/gocli/exitcode"
    "github.com/spiegel-im-spiegel/gocli/rwi"
)

func run(ui *rwi.RWI) exitcode.ExitCode {
    ui.Outputln("Hello world")
    return exitcode.Normal
}

func main() {
    run(rwi.New(
        rwi.WithReader(os.Stdin),
        rwi.WithWriter(os.Stdout),
        rwi.WithErrorWriter(os.Stderr),
    )).Exit()
}
Handling SIGNAL with Context Package
package main

import (
    "context"
    "fmt"
    "os"
    "time"

    "github.com/spiegel-im-spiegel/gocli/signal"
)

func ticker(ctx context.Context) error {
    t := time.NewTicker(1 * time.Second) // 1 second cycle
    defer t.Stop()

    for {
        select {
        case now := <-t.C: // ticker event
            fmt.Println(now.Format(time.RFC3339))
        case <-ctx.Done(): // cancel event from context
            fmt.Println("Stop ticker")
            return ctx.Err()
        }
    }
}

func Run() error {
    errCh := make(chan error, 1)
    defer close(errCh)

    go func() {
        child, cancelChild := context.WithTimeout(
            signal.Context(context.Background(), os.Interrupt), // cancel event by SIGNAL
            10*time.Second, // timeout after 10 seconds
        )
        defer cancelChild()
        errCh <- ticker(child)
    }()

    err := <-errCh
    fmt.Println("Done")
    return err
}

func main() {
    if err := Run(); err != nil {
        fmt.Fprintln(os.Stderr, err)
        return
    }
}
Search Files and Directories (w/ Wildcard)
import (
    "fmt"

    "github.com/spiegel-im-spiegel/gocli/file"
)

result := file.Glob("**/*.[ch]", file.NewGlobOption())
fmt.Println(result)
// Output:
// [testdata/include/source.h testdata/source.c]
Configuration file and directory

Support $XDG_CONFIG_HOME environment value (XDG Base Directory)

import (
    "fmt"

    "github.com/spiegel-im-spiegel/gocli/config"
)

path := config.Path("app", "config.json")
fmt.Println(path)
// Output:
// /home/username/.config/app/config.json
User cache file and directory

Support $XDG_CONFIG_HOME environment value (XDG Base Directory)

import (
    "fmt"

    "github.com/spiegel-im-spiegel/gocli/cache"
)

path := cache.Path("app", "access.log")
fmt.Println(path)
// Output:
// /home/username/.cache/app/access.log

Directories

Path Synopsis
Package cache : User cache file and directory These codes are licensed under CC0.
Package cache : User cache file and directory These codes are licensed under CC0.
Package config : Configuration file and directory These codes are licensed under CC0.
Package config : Configuration file and directory These codes are licensed under CC0.
Package exitcode : OS exit code enumeration These codes are licensed under CC0.
Package exitcode : OS exit code enumeration These codes are licensed under CC0.
Package file : Operating files and directories These codes are licensed under CC0.
Package file : Operating files and directories These codes are licensed under CC0.
Package rwi : Reader/Writer Interface for command-line These codes are licensed under CC0.
Package rwi : Reader/Writer Interface for command-line These codes are licensed under CC0.
Package signal : Handling SIGNAL with context package These codes are licensed under CC0.
Package signal : Handling SIGNAL with context package These codes are licensed under CC0.

Jump to

Keyboard shortcuts

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