clibase

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 3 Imported by: 4

README

📚 CLI Base

Language Go Version GitHub tag (latest by date)

clibase は、Go言語でコマンドラインインターフェース (CLI) アプリケーションを迅速に構築するための、spf13/cobra ベースの共通基盤を提供するパッケージです。

✨ 特徴

  • cobra ベース: 強力なCLI構築ライブラリ spf13/cobra を基盤としています。
  • 構造体による宣言的定義: clibase.App 構造体に必要な要素を渡すだけで、ボイラープレートを排除した綺麗な main 関数を実現します。
  • ライフサイクル管理: PreRunE による初期化に加え、PostRun による確実なリソース解放(クローズ処理など)を標準サポート。
  • 共通フラグの標準提供: verbose (-V) と config (-C) を標準提供。

🛠️ インストール

go get github.com/shouni/clibase


🚀 使用方法

1. アプリケーションの構築と実行

clibase.App を使って、フラグ、チェックロジック、クリーンアップ、サブコマンドを集約します。

// main.go
package main

import (
    "fmt"
    "os"

    "github.com/shouni/clibase"
    "github.com/spf13/cobra"
)

var customAPIKey string

func main() {
    clibase.Execute(clibase.App{
        Name: "my-app",
        
        // (1) アプリ固有の永続フラグを追加
        AddFlags: func(rootCmd *cobra.Command) {
            rootCmd.PersistentFlags().StringVar(&customAPIKey, "api-key", "", "API Key for authentication")
        },

        // (2) 実行前チェック(共通処理の後に実行されます)
        PreRunE: func(cmd *cobra.Command, args []string) error {
            if customAPIKey == "" && os.Getenv("APP_API_KEY") == "" {
                return fmt.Errorf("--api-key or APP_API_KEY environment variable is required")
            }
            return nil
        },

        // (3) 実行後処理(リソースの解放などに使用。NEW!)
        PostRun: func(cmd *cobra.Command, args []string) {
            fmt.Println("Cleaning up resources...")
        },

        // (4) サブコマンドの追加
        Commands: []*cobra.Command{
            helloCmd,
        },
    })
}

var helloCmd = &cobra.Command{
    Use:   "hello",
    Short: "Prints a greeting",
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("Hello!")
        
        // 共通フラグの値をゲッターから取得
        cfg := clibase.GetConfig()
        if cfg.Verbose {
            fmt.Printf("Debug: Config file is %s\n", cfg.ConfigFile)
        }
    },
}


⚙️ 提供される機能

共通フラグ

すべてのコマンドで以下のフラグが自動的に利用可能になります。

フラグ 短縮形 用途 アクセス方法
--verbose -V 詳細ログの有効化 clibase.GetConfig().Verbose
--config -C 設定ファイルのパス指定 clibase.GetConfig().ConfigFile
内部構造: clibase.App

アプリケーションの構成を定義する中心的な構造体です。

type App struct {
    Name     string                                    // アプリ名
    AddFlags func(cmd *cobra.Command)                  // フラグ登録フック
    PreRunE  func(cmd *cobra.Command, args []string) error // 実行前チェック/初期化
    PostRun  func(cmd *cobra.Command, args []string)       // 実行後クリーンアップ
    Commands []*cobra.Command                          // サブコマンド群
}


📜 ライセンス (License)

このプロジェクトは MIT License の下で公開されています。


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute(app App)

Execute は、アプリケーションの構築と実行をワンストップで行います。

Types

type App

type App struct {
	Name     string
	AddFlags func(cmd *cobra.Command)                      // 独自フラグ登録用
	PreRunE  func(cmd *cobra.Command, args []string) error // 実行前バリデーション/初期化用
	PostRun  func(cmd *cobra.Command, args []string)       // 実行後のリソース解放用
	Commands []*cobra.Command                              // サブコマンド群
}

App は CLI アプリケーションの構成を定義します。

type Config

type Config struct {
	Verbose    bool
	ConfigFile string
}

Config は共通フラグの値を保持する内部構造体です。

func GetConfig

func GetConfig() Config

GetConfig は現在の設定情報のコピーを返します。 これにより、利用側は読み取り専用として安全に設定を参照できます。

Jump to

Keyboard shortcuts

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