Move CLI commands to cli/ package for extensibility

- Rename internal/cmd/ to cli/
- Export commands: RootCmd, DropCmd, ConfigCmd
- Enable custom app creation by importing cli package
This commit is contained in:
Dan Jones 2026-02-28 16:45:57 -06:00
commit 8c23375208
4 changed files with 22 additions and 41 deletions

View file

@ -14,7 +14,7 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
package cli
import (
"fmt"
@ -25,11 +25,9 @@ import (
"github.com/spf13/cobra"
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Short: "Save default config to file",
//Long: ``,
var ConfigCmd = &cobra.Command{
Use: "config",
Short: "Save default config to file",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
print, _ := cmd.Flags().GetBool("print")
@ -62,8 +60,8 @@ var configCmd = &cobra.Command{
}
func init() {
rootCmd.AddCommand(configCmd)
RootCmd.AddCommand(ConfigCmd)
configCmd.Flags().BoolP("force", "f", false, "Force overwrite")
configCmd.Flags().BoolP("print", "p", false, "Print path only")
ConfigCmd.Flags().BoolP("force", "f", false, "Force overwrite")
ConfigCmd.Flags().BoolP("print", "p", false, "Print path only")
}

View file

@ -14,7 +14,7 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
package cli
import (
"encoding/json"
@ -34,8 +34,8 @@ var fields map[string]string
var j Json
var outJson bool
// dropCmd represents the drop command
var dropCmd = &cobra.Command{
// DropCmd represents the drop command
var DropCmd = &cobra.Command{
Use: "drop log title",
Short: "Add a new log entry",
// Long: ``,
@ -83,13 +83,13 @@ var dropCmd = &cobra.Command{
}
func init() {
rootCmd.AddCommand(dropCmd)
RootCmd.AddCommand(DropCmd)
(&d).Set("now")
dropCmd.Flags().VarP(&d, "date", "d", "Date for log entry")
dropCmd.Flags().StringToStringVarP(&fields, "fields", "f", nil, "Fields you add to entry")
dropCmd.Flags().VarP(&j, "json", "j", "Entire entry as json")
dropCmd.Flags().BoolVarP(&outJson, "output_json", "o", false, "Output result as JSON")
DropCmd.Flags().VarP(&d, "date", "d", "Date for log entry")
DropCmd.Flags().StringToStringVarP(&fields, "fields", "f", nil, "Fields you add to entry")
DropCmd.Flags().VarP(&j, "json", "j", "Entire entry as json")
DropCmd.Flags().BoolVarP(&outJson, "output_json", "o", false, "Output result as JSON")
}
type Json struct {

View file

@ -14,7 +14,7 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
package cli
import (
"os"
@ -23,36 +23,19 @@ import (
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
var RootCmd = &cobra.Command{
Use: "my-log",
Short: "A brief description of your application",
//Long: ``,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
err := RootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
//cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVarP(&config.ConfigPath, "config", "c", config.ConfigPath, "config file")
rootCmd.PersistentFlags().StringToStringVarP(&config.Overrides, "config-value", "v", config.Overrides, "Override config values. Use dot syntax to specify key. E.g. -v output.stdout.config.format=json")
// Cobra also supports local flags, which will only run
// when this action is called directly.
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
RootCmd.PersistentFlags().StringVarP(&config.ConfigPath, "config", "c", config.ConfigPath, "config file")
RootCmd.PersistentFlags().StringToStringVarP(&config.Overrides, "config-value", "v", config.Overrides, "Override config values. Use dot syntax to specify key. E.g. -v output.stdout.config.format=json")
}

View file

@ -16,8 +16,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import "codeberg.org/danjones000/my-log/internal/cmd"
import "codeberg.org/danjones000/my-log/cli"
func main() {
cmd.Execute()
cli.Execute()
}