2020-04-13 10:29:56 -04:00
|
|
|
/*
|
2023-06-07 22:06:07 -04:00
|
|
|
Copyright © 2020-2023 James Walker <walkah@walkah.net>
|
2020-04-13 10:29:56 -04:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-04-13 22:06:09 -04:00
|
|
|
"errors"
|
2020-04-13 10:29:56 -04:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-01-05 03:06:05 +00:00
|
|
|
"path"
|
2020-04-13 10:29:56 -04:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2021-08-02 17:41:17 -04:00
|
|
|
"github.com/walkah/workon/tmux"
|
2020-04-13 10:29:56 -04:00
|
|
|
|
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cfgFile string
|
|
|
|
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
|
|
var rootCmd = &cobra.Command{
|
2023-11-04 13:01:15 -04:00
|
|
|
Version: "0.2.3",
|
2020-12-29 19:59:08 -05:00
|
|
|
Use: "workon [project]",
|
|
|
|
Short: "Manage tmux sessions",
|
|
|
|
Long: "",
|
2020-04-13 22:06:09 -04:00
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) < 1 {
|
2021-12-19 17:00:40 -05:00
|
|
|
return errors.New("no project or command given")
|
2020-04-13 22:06:09 -04:00
|
|
|
}
|
|
|
|
startCmd.Run(cmd, args)
|
|
|
|
return nil
|
|
|
|
},
|
2021-08-02 17:41:17 -04:00
|
|
|
ValidArgsFunction: completeProjects,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {},
|
|
|
|
}
|
|
|
|
|
|
|
|
func completeProjects(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
2021-12-19 17:00:40 -05:00
|
|
|
projects, err := tmux.ListProjects()
|
2021-08-02 17:41:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
}
|
|
|
|
return projects, cobra.ShellCompDirectiveNoFileComp
|
2020-04-13 10:29:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
2021-01-05 03:06:05 +00:00
|
|
|
ensureConfigDir()
|
2020-04-13 10:29:56 -04:00
|
|
|
|
|
|
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.workon.yaml)")
|
|
|
|
}
|
|
|
|
|
2021-01-05 03:06:05 +00:00
|
|
|
// ensureConfigDir ensures ~/.workon/ exists
|
|
|
|
func ensureConfigDir() {
|
|
|
|
home, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
configDir := path.Join(home, ".workon")
|
|
|
|
if _, err = os.Stat(configDir); os.IsNotExist(err) {
|
|
|
|
os.Mkdir(configDir, 0755)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:29:56 -04:00
|
|
|
// initConfig reads in config file and ENV variables if set.
|
|
|
|
func initConfig() {
|
|
|
|
if cfgFile != "" {
|
|
|
|
// Use config file from the flag.
|
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
} else {
|
|
|
|
// Find home directory.
|
|
|
|
home, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search config in home directory with name ".workon" (without extension).
|
|
|
|
viper.AddConfigPath(home)
|
|
|
|
viper.SetConfigName(".workon")
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
|
|
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
|
|
|
}
|
|
|
|
}
|