2022-05-21 00:49:13 +02:00
|
|
|
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-05-12 15:54:16 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-27 22:28:43 +02:00
|
|
|
"log"
|
2022-05-12 15:54:16 +02:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-05-27 22:14:29 +02:00
|
|
|
// appName is the name of the app.
|
2022-05-12 15:54:16 +02:00
|
|
|
const appName = "go-xkcdreader"
|
|
|
|
|
2022-05-27 22:14:29 +02:00
|
|
|
// Root is the main go-xkcdreader command.
|
2022-05-12 15:54:16 +02:00
|
|
|
var Root = &cobra.Command{
|
|
|
|
Use: appName,
|
|
|
|
Short: "an offline-capable xkcd webcomic reader written in Go",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
// the app will be started from here
|
|
|
|
// a noop atm
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-05-27 22:14:29 +02:00
|
|
|
// GetAppName returns the name of the application.
|
2022-05-12 15:54:16 +02:00
|
|
|
func GetAppName() string {
|
|
|
|
return appName
|
|
|
|
}
|
|
|
|
|
2022-05-27 22:14:29 +02:00
|
|
|
// Execute is the entrypoint of cobra's poison.
|
2022-05-12 15:54:16 +02:00
|
|
|
func Execute() {
|
|
|
|
if err := Root.Execute(); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2022-05-15 11:27:48 +02:00
|
|
|
|
|
|
|
// help redefines the original help func, essentially adding a way to exit the
|
2022-05-27 22:14:29 +02:00
|
|
|
// application properly after showing help message.
|
2022-05-15 11:27:48 +02:00
|
|
|
func help(*cobra.Command, []string) {
|
|
|
|
err := Root.Usage()
|
|
|
|
if err != nil {
|
2022-05-27 22:28:43 +02:00
|
|
|
log.Println(err)
|
2022-05-15 11:27:48 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-05-27 23:08:17 +02:00
|
|
|
|
2022-05-15 11:27:48 +02:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Root.SetHelpFunc(help)
|
|
|
|
}
|