-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_init.go
64 lines (54 loc) · 1.54 KB
/
cmd_init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"os"
"github.com/spf13/cobra"
)
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "initialize a virtual environment in the current directory",
Long: `Initialize a virtual environment in the current directory in .venv directory.
If requirements.txt or similar file is present, it will automatically
install the dependenciesfrom it.`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
requirementsFileFlag, err := cmd.Flags().GetString("requirements-file")
if err != nil {
return err
}
pythonFlag, err := cmd.Flags().GetString("python")
if err != nil {
return err
}
printProgress("Gathering information about script and environment...")
script, err := NewInitCmd(pythonFlag, requirementsFileFlag)
if err != nil {
return err
}
printProgress("Ensuring virtual environment...")
err = script.EnsureEnv(true)
if err != nil {
return err
}
printProgress("Done!")
if !flagDebug {
// Clear all progress messages
printProgress("")
}
// Flush the buffers to preserve the output order and avoid interference
// between the script output and the invenv output
os.Stderr.Sync()
os.Stdout.Sync()
return nil
},
}
func init() {
rootCmd.AddCommand(initCmd)
initCmd.Flags().StringP("requirements-file", "r", "",
`use specified requirements file. If not provided, it
will use requirements.txt`)
initCmd.Flags().StringP("python", "p", "", "use specified Python interpreter")
}