chinampa/internal/commands/version.go

39 lines
902 B
Go
Raw Permalink Normal View History

2022-12-31 05:53:24 +00:00
// Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx>
// SPDX-License-Identifier: Apache-2.0
package commands
import (
"os"
2023-03-20 06:15:53 +00:00
"git.rob.mx/nidito/chinampa/pkg/logger"
2022-12-31 05:53:24 +00:00
"git.rob.mx/nidito/chinampa/pkg/statuscode"
"github.com/spf13/cobra"
)
2023-01-23 04:29:18 +00:00
var VersionCommandName = "version"
2022-12-31 05:53:24 +00:00
var Version = &cobra.Command{
2023-01-23 04:29:18 +00:00
Use: VersionCommandName,
2022-12-31 05:53:24 +00:00
Short: "Display program version",
Hidden: false,
DisableAutoGenTag: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
output := cmd.ErrOrStderr()
version := cmd.Root().Annotations["version"]
if cmd.CalledAs() == "" {
// user asked for --version directly
output = cmd.OutOrStderr()
version += "\n"
}
_, err := output.Write([]byte(version))
if err != nil {
2023-03-20 06:15:53 +00:00
logger.Main.Errorf("version error: %s", err)
2022-12-31 05:53:24 +00:00
return err
}
os.Exit(statuscode.Ok)
return nil
},
}