pkg.tk-software.de/gotice@v0.4.1-0.20240224130243-6adec687b106/main.go (about)

     1  // Copyright 2023-2024 Tobias Koch. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  )
    11  
    12  // The available subcommands.
    13  var commands = []Runner{
    14  	NewInitCommand(),
    15  	NewGenerateCommand(),
    16  	NewVersionCommand(),
    17  	NewHelpCommand(),
    18  }
    19  
    20  func exec(args []string) error {
    21  	if len(args) < 2 {
    22  		return ErrMissingSubcommand
    23  	}
    24  
    25  	subcommand := args[1]
    26  	for _, c := range commands {
    27  		if subcommand == c.Name() {
    28  			if err := c.Init(args[2:]); err != nil {
    29  				return fmt.Errorf("unable to initialize subcommand %s: %w", subcommand, err)
    30  			}
    31  
    32  			return c.Run()
    33  		}
    34  	}
    35  
    36  	return fmt.Errorf("unknown subcommand %s", subcommand)
    37  }
    38  
    39  func main() {
    40  	if err := exec(os.Args); err != nil {
    41  		fmt.Fprintf(os.Stderr, "Error: %s\n", err)
    42  		os.Exit(1)
    43  	}
    44  }