概要
Go言語を勉強中の者です。
ストップウォッチ機能を作成しています。
CLIで動作するため、Cobraを利用して開発しています。
利用性向上のために、開発者側で設定したコマンド名のエイリアスを作成したいのですが、
実装方法がわからず停滞しています。
Cobraを利用している記事等を見ると、go installでエイリアスが作成されるようですが、
Command not foundとなってしまいます。
実現したいこと
-
go run main.go stopwatchではなく、go-timer stopwatchで動作するようにする。
発生している問題・エラーメッセージ
go-timer: command not found
該当のソースコード
golang
1/* 2Copyright © 2022 NAME HERE <EMAIL ADDRESS> 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16package cmd 17 18import ( 19 "fmt" 20 "os" 21 22 "github.com/spf13/cobra" 23 24 homedir "github.com/mitchellh/go-homedir" 25 "github.com/spf13/viper" 26) 27 28var cfgFile string 29 30// rootCmd represents the base command when called without any subcommands 31var rootCmd = &cobra.Command{ 32 Use: "goTimer", 33 Short: "ストップウォッチの機能を提供しています。", 34 Long: `ストップウォッチの機能を提供しています。 35 36 go-timer stopwatch で利用可能です。 37 終了時はCtrl+Cを入力してください。`, 38 // Uncomment the following line if your bare application 39 // has an action associated with it: 40 // Run: func(cmd *cobra.Command, args []string) { }, 41} 42 43// Execute adds all child commands to the root command and sets flags appropriately. 44// This is called by main.main(). It only needs to happen once to the rootCmd. 45func Execute() { 46 if err := rootCmd.Execute(); err != nil { 47 fmt.Println(err) 48 os.Exit(1) 49 } 50} 51 52func init() { 53 cobra.OnInitialize(initConfig) 54 55 // Here you will define your flags and configuration settings. 56 // Cobra supports persistent flags, which, if defined here, 57 // will be global for your application. 58 59 rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.go-timer.yaml)") 60 61 // Cobra also supports local flags, which will only run 62 // when this action is called directly. 63 rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") 64} 65 66// initConfig reads in config file and ENV variables if set. 67func initConfig() { 68 if cfgFile != "" { 69 // Use config file from the flag. 70 viper.SetConfigFile(cfgFile) 71 } else { 72 // Find home directory. 73 home, err := homedir.Dir() 74 if err != nil { 75 fmt.Println(err) 76 os.Exit(1) 77 } 78 79 // Search config in home directory with name ".go-timer" (without extension). 80 viper.AddConfigPath(home) 81 viper.SetConfigName(".go-timer") 82 } 83 84 viper.AutomaticEnv() // read in environment variables that match 85 86 // If a config file is found, read it in. 87 if err := viper.ReadInConfig(); err == nil { 88 fmt.Println("Using config file:", viper.ConfigFileUsed()) 89 } 90}
試したこと
bash
1go-timer$ go build 2go-timer$ go install
補足情報(FW/ツールのバージョンなど)
requirements
ubuntu20.04.01 LTS
go1.17.8 linux/amd64
github.com/spf13/cobra v1.3.0
ソースコード
Cobraのソースコード
私のソースコード
go build や go install で生成されるのはエイリアスではなく実行形式 (実行ファイル) です。go run だと毎回コンパイルしてから動かすことになり無駄なので、プログラムが完成したらビルドして実行形式を動かすのが一般的です。
go build すれば go-timer という名前の実行形式がプロジェクトフォルダに作られるので、./go-timer stopwatch で動きます。そして、go install すれば $GOBIN または $GOPATH/bin または $HOME/go/bin に作られるので、そこにパスを通せば go-timer stopwatch で動くと思います。
https://blog.applibot.co.jp/2021/04/20/go-install/
あと、Ctrl+C は通常プログラムを強制終了するために使われるので、ストップウォッチの計測終了という正常な処理を Ctrl+C に割り当てるのはちょっとどうかと…。
この辺は Go 以前に普通の Linux の使い方の話なので、その辺をおさらいすることをお勧めします。
ご指摘ありがとうございます。
本題の件はhoshi-takanori様のご指摘をもとに見直したところ、無事解決に至りました。
また、ストップウォッチの終了処理についても、今後勘案して開発してみたいと思います。
ありがとうございました。
回答1件
あなたの回答
tips
プレビュー