|
| 1 | +// Copyright © 2017 NAME HERE <EMAIL ADDRESS> |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package cmd |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + |
| 19 | + "github.com/spf13/cobra" |
| 20 | + "os/exec" |
| 21 | + "github.com/caojia/gip/log" |
| 22 | + "runtime" |
| 23 | +) |
| 24 | + |
| 25 | +var direnvVersion = "v2.12.2" |
| 26 | + |
| 27 | +func setupBash() { |
| 28 | + exec.Command("echo", "'eval $(direnv hook bash)'", ">>", "~/.bashrc") |
| 29 | +} |
| 30 | + |
| 31 | +func setupZsh() { |
| 32 | + exec.Command("echo", "'eval $(direnv hook zsh)'", ">>", "~/.zshrc") |
| 33 | +} |
| 34 | + |
| 35 | +func setupDarwin() { |
| 36 | + _, err := exec.Command("command", "-v", "brew").Output() |
| 37 | + // if brew is installed, use brew, otherwise, use default |
| 38 | + if err == nil { |
| 39 | + output, err := exec.Command("brew", "install", "direnv").CombinedOutput() |
| 40 | + if err != nil { |
| 41 | + log.Error(string(output)) |
| 42 | + return |
| 43 | + } |
| 44 | + } else { |
| 45 | + setupDefault() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func setupDefault() { |
| 50 | + //https://github.com/direnv/direnv/releases/download/v2.12.2/direnv.darwin-386 |
| 51 | + url := fmt.Sprintf("https://github.com/direnv/direnv/releases/download/%s/direnv.%s-%s", |
| 52 | + direnvVersion, runtime.GOOS, runtime.GOARCH) |
| 53 | + exec.Command("mkdir", "-p", "~/bin") |
| 54 | + exec.Command("wget", "-O", "~/bin/direnv", url) |
| 55 | + exec.Command("chmod", "+x", "~/bin/direnv") |
| 56 | +} |
| 57 | + |
| 58 | +// setupCmd represents the setup command |
| 59 | +var setupCmd = &cobra.Command{ |
| 60 | + Use: "setup", |
| 61 | + Short: "Install and setup the required commands.", |
| 62 | + Long: `Install and setup the required commands. Including: |
| 63 | +
|
| 64 | +- install direnv |
| 65 | +- setup direnv for bash/zsh`, |
| 66 | + Run: func(cmd *cobra.Command, args []string) { |
| 67 | + _, err := exec.Command("command", "-v", "direnv").Output() |
| 68 | + if err == nil { |
| 69 | + log.Info(`direnv is already installed, please make sure it is set up properly. |
| 70 | +
|
| 71 | +For more information, check out: https://github.com/direnv/direnv |
| 72 | + `) |
| 73 | + return |
| 74 | + } |
| 75 | + switch runtime.GOOS { |
| 76 | + case "darwin": |
| 77 | + setupDarwin() |
| 78 | + case "windows": |
| 79 | + panic("Sorry, we don't support windows yet.") |
| 80 | + default: |
| 81 | + setupDefault() |
| 82 | + } |
| 83 | + |
| 84 | + setupBash() |
| 85 | + setupZsh() |
| 86 | + log.Info(`If you're not using bash or zsh, please follow https://github.com/direnv/direnv to setup direnv for your shell.`) |
| 87 | + }, |
| 88 | +} |
| 89 | + |
| 90 | +func init() { |
| 91 | + RootCmd.AddCommand(setupCmd) |
| 92 | + |
| 93 | + // Here you will define your flags and configuration settings. |
| 94 | + |
| 95 | + // Cobra supports Persistent Flags which will work for this command |
| 96 | + // and all subcommands, e.g.: |
| 97 | + // setupCmd.PersistentFlags().String("foo", "", "A help for foo") |
| 98 | + |
| 99 | + // Cobra supports local flags which will only run when this command |
| 100 | + // is called directly, e.g.: |
| 101 | + // setupCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") |
| 102 | +} |
0 commit comments