├── .gitignore ├── main.go ├── cmd ├── version.go ├── printer │ └── output.go ├── create-options.go ├── sshkeys.go ├── account.go ├── root.go ├── transaction.go └── server.go ├── README.md └── LICENSE.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | bin/ 3 | pkg/ 4 | dist/ 5 | src/ 6 | blcli 7 | .DS_Store -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 The blcli Authors All rights reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | */ 13 | package main 14 | 15 | import ( 16 | "./cmd" 17 | ) 18 | 19 | const ( 20 | version = "1.1.0" 21 | ) 22 | 23 | func main() { 24 | cmd.Execute() 25 | } 26 | -------------------------------------------------------------------------------- /cmd/version.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 The blcli Authors All rights reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | */ 13 | 14 | package cmd 15 | 16 | import ( 17 | "fmt" 18 | 19 | "github.com/spf13/cobra" 20 | ) 21 | 22 | var versionCmd = &cobra.Command{ 23 | Use: "version", 24 | Short: "blcli version", 25 | Long: `Print the version number of blcli`, 26 | Run: func(cmd *cobra.Command, args []string) { 27 | fmt.Println("blcli 1.1.0") 28 | }, 29 | } 30 | -------------------------------------------------------------------------------- /cmd/printer/output.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 The blcli Authors All rights reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | */ 13 | 14 | package printer 15 | 16 | import ( 17 | "encoding/json" 18 | "errors" 19 | "fmt" 20 | ) 21 | 22 | // Format is the type of output to display 23 | var Format string 24 | 25 | // Output writes the output 26 | func Output(data interface{}) { 27 | var err error 28 | switch Format { 29 | case "json": 30 | err = writeJSON(data) 31 | default: 32 | err = errors.New("unknown output format") 33 | } 34 | 35 | if err != nil { 36 | fmt.Printf("Error: %s\n", err) 37 | } 38 | } 39 | 40 | func writeJSON(data interface{}) error { 41 | j, err := json.MarshalIndent(data, "", " ") 42 | if err != nil { 43 | return err 44 | } 45 | fmt.Println(string(j)) 46 | return nil 47 | } 48 | -------------------------------------------------------------------------------- /cmd/create-options.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 The blcli Authors All rights reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | */ 13 | 14 | package cmd 15 | 16 | import ( 17 | "errors" 18 | "fmt" 19 | "os" 20 | 21 | "./printer" 22 | "github.com/spf13/cobra" 23 | ) 24 | 25 | // CreateOptions sets up the create options command and subcommands 26 | func CreateOptions() *cobra.Command { 27 | cmd := &cobra.Command{ 28 | Use: "create-options ", 29 | Short: "View images, sizes, and options available for a host when creating a new server.", 30 | Long: ``, 31 | Aliases: []string{"o"}, 32 | Args: func(cmd *cobra.Command, args []string) error { 33 | if len(args) < 1 { 34 | return errors.New("please provide a host name: bitlaunch, digitalocean, vultr, or linode") 35 | } 36 | return nil 37 | }, 38 | Run: func(cmd *cobra.Command, args []string) { 39 | id := args[0] 40 | hid, err := hostID(id) 41 | if err != nil { 42 | fmt.Println(err) 43 | os.Exit(1) 44 | } 45 | server, err := client.CreateOptions.Show(hid) 46 | if err != nil { 47 | fmt.Printf("Error getting server : %v\n", err) 48 | os.Exit(1) 49 | } 50 | 51 | printer.Output(server) 52 | }, 53 | } 54 | return cmd 55 | } 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # blcli 2 | 3 | blcli is a command-line interface for BitLaunch.io 4 | 5 | ``` 6 | Usage: 7 | blcli [command] 8 | 9 | Available Commands: 10 | account Retrieve account information 11 | create-options View images, sizes, and options available for a host when creating a new server. 12 | help Help about any command 13 | server Manage your virtual machines 14 | sshkey Manage SSH Keys 15 | transaction Manage transactions 16 | version blcli version 17 | 18 | Flags: 19 | --config string config file (default is $HOME/.blcli.yaml) 20 | -h, --help help for blcli 21 | --token string API authentication token 22 | 23 | Use "blcli [command] --help" for more information about a command. 24 | ``` 25 | 26 | ## Installing `blcli` 27 | 28 | ### Downloading a Release 29 | 30 | Visit the [Releases 31 | page](https://github.com/bitlaunchio/blcli/releases) for the 32 | [`blcli` GitHub project](https://github.com/bitlaunchio/blcli). 33 | 34 | You can optionally move the `blcli` binary to your path. For example: 35 | 36 | ```sh 37 | sudo mv ~/blcli /usr/local/bin 38 | ``` 39 | 40 | ### Building from source 41 | ```sh 42 | git clone https://github.com/bitlaunchio/blcli.git 43 | cd blcli 44 | go get . 45 | go build . 46 | ``` 47 | 48 | ## Authentication 49 | 50 | To use `blcli` you'll need an API access token. You can generate one in your BitLaunch account [API page](https://app.bitlaunch.io/account/api). More information is available at the [developer hub](https://developers.bitlaunch.io). 51 | 52 | Once you have your token, you can either: 53 | 54 | 1. Specify it with each request: 55 | 56 | ```sh 57 | blcli --token TOKEN_HERE ... 58 | ``` 59 | 60 | 2. Set it as an environment variable: 61 | 62 | ```sh 63 | export BL_API_TOKEN=TOKEN_HERE 64 | ``` 65 | 66 | ## Examples 67 | 68 | Here are a few examples of using `blcli`. More help is available with `blcli [command] -h` and further documentation is available at the [developer hub](https://developers.bitlaunch.io/) 69 | 70 | * View your account and balance: 71 | ```sh 72 | blcli account 73 | ``` 74 | * View your account usage: 75 | ```sh 76 | blcli account usage --period 2020-09 77 | ``` 78 | * View your account history/activity: 79 | ```sh 80 | blcli account history 81 | ``` 82 | * List all servers on your account: 83 | ```sh 84 | blcli server list 85 | ``` 86 | * Create a server: 87 | ```sh 88 | blcli server create --host bitlaunch --name test --region lon1 --image 10002 --size nibble-1024 --password b1Tl4uNCH! 89 | ``` 90 | * Restart a server: 91 | ```sh 92 | blcli server restart aaaaaaaaaaabbbbbbbbbbbbb 93 | ``` 94 | * Rebuild a server: 95 | ```sh 96 | blcli server rebuild aaaaaaaaaaabbbbbbbbbbbbb --image 10000 --description "Ubuntu 18.04 LTS" 97 | ``` 98 | * Resize a server: 99 | ```sh 100 | blcli server resize aaaaaaaaaaabbbbbbbbbbbbb --size nibble-2048 101 | ``` 102 | * Create a new Lightning Network transaction: 103 | ```sh 104 | blcli transaction create 20 BTC --lightning 105 | ``` 106 | 107 | -------------------------------------------------------------------------------- /cmd/sshkeys.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 The blcli Authors All rights reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | */ 13 | 14 | package cmd 15 | 16 | import ( 17 | "errors" 18 | "fmt" 19 | "os" 20 | 21 | "./printer" 22 | "github.com/bitlaunchio/gobitlaunch" 23 | 24 | "github.com/spf13/cobra" 25 | ) 26 | 27 | // SSHKey sets up the ssh key command and subcommands 28 | func SSHKey() *cobra.Command { 29 | cmd := &cobra.Command{ 30 | Use: "sshkey", 31 | Short: "Manage SSH Keys", 32 | Long: `Use the subcommands to list, create, or delete ssh keys.`, 33 | Aliases: []string{"k"}, 34 | } 35 | 36 | cmd.AddCommand(sshKeyList) 37 | cmd.AddCommand(sshKeyDelete) 38 | cmd.AddCommand(sshKeyCreate) 39 | 40 | sshKeyCreate.Flags().StringP("name", "n", "", "name for the new key") 41 | sshKeyCreate.Flags().StringP("content", "c", "", "ssh key content") 42 | sshKeyCreate.MarkFlagRequired("name") 43 | sshKeyCreate.MarkFlagRequired("content") 44 | 45 | return cmd 46 | } 47 | 48 | var sshKeyList = &cobra.Command{ 49 | Use: "list", 50 | Short: "List ssh keys on your account", 51 | Long: ``, 52 | Aliases: []string{"l"}, 53 | Run: func(cmd *cobra.Command, args []string) { 54 | servers, err := client.SSHKey.List() 55 | if err != nil { 56 | fmt.Printf("Error listing ssh keys : %v\n", err) 57 | os.Exit(1) 58 | } 59 | 60 | printer.Output(servers) 61 | }, 62 | } 63 | 64 | var sshKeyDelete = &cobra.Command{ 65 | Use: "delete", 66 | Short: "Permanently delete an ssh key", 67 | Long: `delete `, 68 | Aliases: []string{"delete", "d", "del", "rm"}, 69 | Args: func(cmd *cobra.Command, args []string) error { 70 | if len(args) < 1 { 71 | return errors.New("please provide am ssh key ID") 72 | } 73 | return nil 74 | }, 75 | Run: func(cmd *cobra.Command, args []string) { 76 | id := args[0] 77 | err := client.SSHKey.Delete(id) 78 | if err != nil { 79 | fmt.Printf("Error deleting ssh key : %v\n", err) 80 | os.Exit(1) 81 | } 82 | 83 | fmt.Println("Deleted ssh key") 84 | }, 85 | } 86 | 87 | var sshKeyCreate = &cobra.Command{ 88 | Use: "create", 89 | Short: "Create a new ssh key", 90 | Long: ``, 91 | Aliases: []string{"c"}, 92 | Run: func(cmd *cobra.Command, args []string) { 93 | opts := gobitlaunch.SSHKey{} 94 | opts.Name, _ = cmd.Flags().GetString("name") 95 | opts.Content, _ = cmd.Flags().GetString("content") 96 | 97 | key, err := client.SSHKey.Create(&opts) 98 | if err != nil { 99 | fmt.Printf("Error creating ssh key : %v\n", err) 100 | os.Exit(1) 101 | } 102 | 103 | printer.Output(key) 104 | }, 105 | } 106 | -------------------------------------------------------------------------------- /cmd/account.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 The blcli Authors All rights reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | */ 13 | 14 | package cmd 15 | 16 | import ( 17 | "fmt" 18 | "os" 19 | 20 | "./printer" 21 | 22 | "github.com/spf13/cobra" 23 | ) 24 | 25 | // Account sets up the account command 26 | func Account() *cobra.Command { 27 | cmd := &cobra.Command{ 28 | Use: "account", 29 | Short: "Retrieve account information", 30 | Long: `Use the subcommands to display information about your account.`, 31 | Run: func(cmd *cobra.Command, args []string) { 32 | if len(args) == 0 { 33 | account, err := client.Account.Show() 34 | if err != nil { 35 | fmt.Printf("Error getting account information : %v", err) 36 | os.Exit(1) 37 | } 38 | 39 | printer.Output(account) 40 | } 41 | }, 42 | } 43 | 44 | cmd.AddCommand(accountShow) 45 | cmd.AddCommand(accountUsage) 46 | cmd.AddCommand(accountHistory) 47 | 48 | accountUsage.Flags().StringP("period", "p", "latest", "filter for period, format: YYYY-MM or latest") 49 | 50 | accountHistory.Flags().IntP("page", "p", 1, "page number of history results to show") 51 | accountHistory.Flags().IntP("items", "i", 25, "how many history results to show") 52 | 53 | return cmd 54 | } 55 | 56 | var accountShow = &cobra.Command{ 57 | Use: "show", 58 | Short: "Retrieve account information", 59 | Long: ``, 60 | Run: func(cmd *cobra.Command, args []string) { 61 | account, err := client.Account.Show() 62 | if err != nil { 63 | fmt.Printf("Error getting account information : %v", err) 64 | os.Exit(1) 65 | } 66 | 67 | printer.Output(account) 68 | }, 69 | } 70 | 71 | var accountUsage = &cobra.Command{ 72 | Use: "usage", 73 | Short: "Retrieve account usage information", 74 | Long: ``, 75 | Run: func(cmd *cobra.Command, args []string) { 76 | period, _ := cmd.Flags().GetString("period") 77 | 78 | usage, err := client.Account.Usage(period) 79 | 80 | if err != nil { 81 | fmt.Printf("Error getting account usage information : %v", err) 82 | os.Exit(1) 83 | } 84 | 85 | printer.Output(usage) 86 | }, 87 | } 88 | 89 | var accountHistory = &cobra.Command{ 90 | Use: "history", 91 | Short: "Retrieve account history information", 92 | Long: ``, 93 | Run: func(cmd *cobra.Command, args []string) { 94 | page, _ := cmd.Flags().GetInt("page") 95 | items, _ := cmd.Flags().GetInt("items") 96 | 97 | history, err := client.Account.History(page, items) 98 | if err != nil { 99 | fmt.Printf("Error getting account history information : %v", err) 100 | os.Exit(1) 101 | } 102 | 103 | printer.Output(history) 104 | }, 105 | } 106 | -------------------------------------------------------------------------------- /cmd/root.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 The blcli Authors All rights reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | */ 13 | 14 | package cmd 15 | 16 | import ( 17 | "errors" 18 | "fmt" 19 | "os" 20 | 21 | "./printer" 22 | "github.com/bitlaunchio/gobitlaunch" 23 | 24 | homedir "github.com/mitchellh/go-homedir" 25 | "github.com/spf13/cobra" 26 | "github.com/spf13/viper" 27 | ) 28 | 29 | var ( 30 | client *gobitlaunch.Client 31 | cfgFile string 32 | token string 33 | format string 34 | 35 | rootCmd = &cobra.Command{ 36 | Use: "blcli", 37 | Short: "blcli is a command-line interface for BitLaunch.io", 38 | Long: ``, 39 | } 40 | ) 41 | 42 | func hostID(name string) (int, error) { 43 | var err error 44 | var h int 45 | switch name { 46 | case "bitlaunch", "bl": 47 | h = 4 48 | case "digitalocean", "do": 49 | h = 0 50 | case "vultr", "v": 51 | h = 1 52 | case "linode", "l": 53 | h = 2 54 | default: 55 | err = errors.New("invalid host") 56 | } 57 | return h, err 58 | } 59 | 60 | // Execute executes the root command. 61 | func Execute() error { 62 | return rootCmd.Execute() 63 | } 64 | 65 | func init() { 66 | cobra.OnInitialize(initConfig) 67 | cobra.OnInitialize(initClient) 68 | cobra.OnInitialize(initPrinter) 69 | 70 | rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.blcli.yaml)") 71 | rootCmd.PersistentFlags().StringVar(&token, "token", "", "API authentication token") 72 | //rootCmd.PersistentFlags().StringVar(&format, "format", "json", "output format. can be: kv, csv or json (default)") 73 | rootCmd.MarkFlagRequired("token") 74 | 75 | rootCmd.AddCommand(versionCmd) 76 | rootCmd.AddCommand(Account()) 77 | rootCmd.AddCommand(Server()) 78 | rootCmd.AddCommand(Transaction()) 79 | rootCmd.AddCommand(CreateOptions()) 80 | rootCmd.AddCommand(SSHKey()) 81 | } 82 | 83 | func er(msg interface{}) { 84 | fmt.Println("Error:", msg) 85 | os.Exit(1) 86 | } 87 | 88 | func initConfig() { 89 | if cfgFile != "" { 90 | // Use config file from the flag. 91 | viper.SetConfigFile(cfgFile) 92 | } else { 93 | // Find home directory. 94 | home, err := homedir.Dir() 95 | if err != nil { 96 | er(err) 97 | } 98 | 99 | // Search config in home directory with name ".blcli" (without extension). 100 | viper.AddConfigPath(home) 101 | viper.SetConfigName(".blcli") 102 | } 103 | 104 | viper.AutomaticEnv() 105 | 106 | if err := viper.ReadInConfig(); err == nil { 107 | fmt.Println("Using config file:", viper.ConfigFileUsed()) 108 | } 109 | } 110 | 111 | func initClient() { 112 | if versionCmd.CalledAs() == "version" { 113 | return 114 | } 115 | if len(token) == 0 { 116 | token = os.Getenv("BL_API_TOKEN") 117 | if len(token) == 0 { 118 | fmt.Println("You must specify your API token with either the --token parameter or by exporting it as an environment variable:") 119 | fmt.Println("export BL_API_TOKEN=''") 120 | os.Exit(1) 121 | } 122 | } 123 | 124 | client = gobitlaunch.NewClient(token) 125 | } 126 | 127 | func initPrinter() { 128 | printer.Format = "json" 129 | } 130 | -------------------------------------------------------------------------------- /cmd/transaction.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 The blcli Authors All rights reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | */ 13 | 14 | package cmd 15 | 16 | import ( 17 | "errors" 18 | "fmt" 19 | "os" 20 | "strconv" 21 | 22 | "./printer" 23 | "github.com/bitlaunchio/gobitlaunch" 24 | "github.com/mdp/qrterminal" 25 | "github.com/spf13/cobra" 26 | ) 27 | 28 | // Transaction sets up the server command and subcommands 29 | func Transaction() *cobra.Command { 30 | cmd := &cobra.Command{ 31 | Use: "transaction", 32 | Short: "Manage transactions", 33 | Long: `Use the subcommands to get, list, or create transactions.`, 34 | Aliases: []string{"t"}, 35 | } 36 | 37 | cmd.AddCommand(transactionCreate) 38 | cmd.AddCommand(transactionList) 39 | cmd.AddCommand(transactionGet) 40 | cmd.AddCommand(transactionQRCode) 41 | transactionGet.Flags().Bool("qr", false, "output transaction as qr code to terminal") 42 | transactionCreate.Flags().Bool("qr", false, "output transaction as qr code to terminal") 43 | transactionCreate.Flags().BoolP("lightning", "l", false, "optionally use lightning network valid for BTC and LTC up to 0.042 BTC or equivalent.") 44 | transactionList.Flags().IntP("page", "p", 1, "page number") 45 | transactionList.Flags().IntP("items", "i", 25, "number of items per page") 46 | 47 | return cmd 48 | } 49 | 50 | var transactionCreate = &cobra.Command{ 51 | Use: "create", 52 | Short: "Create a new transaction", 53 | Long: `create `, 54 | Aliases: []string{"c", "create"}, 55 | Args: func(cmd *cobra.Command, args []string) error { 56 | if len(args) < 2 { 57 | return errors.New("create ") 58 | } 59 | return nil 60 | }, 61 | Run: func(cmd *cobra.Command, args []string) { 62 | usd := args[0] 63 | symbol := args[1] 64 | usdInt, err := strconv.Atoi(usd) 65 | if err != nil { 66 | fmt.Println("Please specify USD as an integer") 67 | os.Exit(1) 68 | } 69 | ln, _ := cmd.Flags().GetBool("lightning") 70 | if ln && (symbol != "BTC" && symbol != "LTC") { 71 | fmt.Println("Lightning network only available for BTC and LTC") 72 | os.Exit(1) 73 | } 74 | transaction, err := client.Transaction.Create(&gobitlaunch.CreateTransactionOptions{ 75 | AmountUSD: usdInt, 76 | CryptoSymbol: symbol, 77 | LightningNetwork: ln, 78 | }) 79 | if err != nil { 80 | fmt.Printf("Error creating a new transaction : %v\n", err) 81 | os.Exit(1) 82 | } 83 | 84 | qr, _ := cmd.Flags().GetBool("qr") 85 | if qr { 86 | if len(transaction.Address) == 0 || len(transaction.AmountCrypto) == 0 { 87 | fmt.Println("Unable to generate a QR Code for this type of transaction.") 88 | os.Exit(1) 89 | } 90 | s := fmt.Sprintf("bitcoin:%s?amount=%s", transaction.Address, transaction.AmountCrypto) 91 | qrterminal.Generate(s, qrterminal.L, os.Stdout) 92 | return 93 | } 94 | 95 | printer.Output(transaction) 96 | }, 97 | } 98 | 99 | var transactionGet = &cobra.Command{ 100 | Use: "get", 101 | Short: "Get information for a single transaction", 102 | Long: `get `, 103 | Aliases: []string{"g", "show"}, 104 | Args: func(cmd *cobra.Command, args []string) error { 105 | if len(args) < 1 { 106 | return errors.New("please provide a transaction ID") 107 | } 108 | return nil 109 | }, 110 | Run: func(cmd *cobra.Command, args []string) { 111 | id := args[0] 112 | transaction, err := client.Transaction.Show(id) 113 | if err != nil { 114 | fmt.Printf("Error getting transaction : %v\n", err) 115 | os.Exit(1) 116 | } 117 | 118 | qr, _ := cmd.Flags().GetBool("qr") 119 | if qr { 120 | if len(transaction.Address) == 0 || len(transaction.AmountCrypto) == 0 { 121 | fmt.Println("Unable to generate a QR Code for this type of transaction.") 122 | os.Exit(1) 123 | } 124 | s := fmt.Sprintf("bitcoin:%s?amount=%s", transaction.Address, transaction.AmountCrypto) 125 | qrterminal.Generate(s, qrterminal.L, os.Stdout) 126 | return 127 | } 128 | 129 | printer.Output(transaction) 130 | }, 131 | } 132 | 133 | var transactionList = &cobra.Command{ 134 | Use: "list", 135 | Short: "List transactions on your account", 136 | Long: `list --page [page-number|1] --items [items-per-page|25]`, 137 | Aliases: []string{"l"}, 138 | Run: func(cmd *cobra.Command, args []string) { 139 | page, _ := cmd.Flags().GetInt("page") 140 | items, _ := cmd.Flags().GetInt("items") 141 | transactions, err := client.Transaction.List(page, items) 142 | if err != nil { 143 | fmt.Printf("Error listing transactions : %v\n", err) 144 | os.Exit(1) 145 | } 146 | 147 | printer.Output(transactions) 148 | }, 149 | } 150 | 151 | var transactionQRCode = &cobra.Command{ 152 | Use: "qr", 153 | Short: "Generate a QR code for a transaction", 154 | Long: `qr `, 155 | Args: func(cmd *cobra.Command, args []string) error { 156 | if len(args) < 1 { 157 | return errors.New("please provide a transaction ID") 158 | } 159 | return nil 160 | }, 161 | Run: func(cmd *cobra.Command, args []string) { 162 | id := args[0] 163 | transaction, err := client.Transaction.Show(id) 164 | if err != nil { 165 | fmt.Printf("Error getting transaction : %v\n", err) 166 | os.Exit(1) 167 | } 168 | 169 | if len(transaction.Address) == 0 || len(transaction.AmountCrypto) == 0 { 170 | fmt.Println("Unable to generate a QR Code for this type of transaction.") 171 | os.Exit(1) 172 | } 173 | 174 | s := fmt.Sprintf("bitcoin:%s?amount=%s", transaction.Address, transaction.AmountCrypto) 175 | 176 | qrterminal.Generate(s, qrterminal.L, os.Stdout) 177 | }, 178 | } 179 | -------------------------------------------------------------------------------- /cmd/server.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 The blcli Authors All rights reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | */ 13 | 14 | package cmd 15 | 16 | import ( 17 | "errors" 18 | "fmt" 19 | "os" 20 | "strconv" 21 | "strings" 22 | 23 | "./printer" 24 | "github.com/bitlaunchio/gobitlaunch" 25 | 26 | "github.com/spf13/cobra" 27 | ) 28 | 29 | // Server sets up the server command and subcommands 30 | func Server() *cobra.Command { 31 | cmd := &cobra.Command{ 32 | Use: "server", 33 | Short: "Manage your virtual machines", 34 | Long: `Use the subcommands to get, list, create, or destroy servers.`, 35 | Aliases: []string{"s"}, 36 | } 37 | 38 | cmd.AddCommand(serverGet) 39 | cmd.AddCommand(serverList) 40 | cmd.AddCommand(serverDestroy) 41 | cmd.AddCommand(serverCreate) 42 | cmd.AddCommand(serverRebuild) 43 | cmd.AddCommand(serverResize) 44 | cmd.AddCommand(serverRestart) 45 | cmd.AddCommand(serverProtection) 46 | cmd.AddCommand(serverSetPorts) 47 | 48 | serverCreate.Flags().StringP("name", "n", "", "name for the new server") 49 | serverCreate.Flags().StringP("host", "t", "", "target provider/host name: bitlaunch, digitalocean, vultr or linode") 50 | serverCreate.Flags().StringP("image", "i", "", "image/app id") 51 | serverCreate.Flags().StringP("size", "s", "", "plan/size id") 52 | serverCreate.Flags().StringP("region", "r", "", "region id") 53 | serverCreate.Flags().StringSliceP("sshkey", "k", []string{}, "ssh key ids, comma separated for more than one") 54 | serverCreate.Flags().StringP("password", "p", "", "password") 55 | 56 | serverRebuild.Flags().StringP("image", "i", "", "image/app id") 57 | serverRebuild.Flags().StringP("description", "d", "", "image/app description") 58 | 59 | serverResize.Flags().StringP("size", "s", "", "plan/size id") 60 | 61 | serverSetPorts.Flags().StringP("ports", "p", "", "port:protocol, comma separated for more than one") 62 | 63 | serverCreate.MarkFlagRequired("name") 64 | serverCreate.MarkFlagRequired("host") 65 | serverCreate.MarkFlagRequired("image") 66 | serverCreate.MarkFlagRequired("size") 67 | serverCreate.MarkFlagRequired("region") 68 | 69 | serverRebuild.MarkFlagRequired("image") 70 | serverRebuild.MarkFlagRequired("description") 71 | 72 | serverResize.MarkFlagRequired("size") 73 | 74 | serverSetPorts.MarkFlagRequired("ports") 75 | 76 | return cmd 77 | } 78 | 79 | var serverGet = &cobra.Command{ 80 | Use: "get", 81 | Short: "Get information for a single server", 82 | Long: `get `, 83 | Aliases: []string{"g", "show"}, 84 | Args: func(cmd *cobra.Command, args []string) error { 85 | if len(args) < 1 { 86 | return errors.New("please provide a server ID") 87 | } 88 | return nil 89 | }, 90 | Run: func(cmd *cobra.Command, args []string) { 91 | id := args[0] 92 | server, err := client.Server.Show(id) 93 | if err != nil { 94 | fmt.Printf("Error getting server : %v\n", err) 95 | os.Exit(1) 96 | } 97 | 98 | printer.Output(server) 99 | }, 100 | } 101 | 102 | var serverList = &cobra.Command{ 103 | Use: "list", 104 | Short: "List servers on your account", 105 | Long: ``, 106 | Aliases: []string{"l"}, 107 | Run: func(cmd *cobra.Command, args []string) { 108 | servers, err := client.Server.List() 109 | if err != nil { 110 | fmt.Printf("Error listing servers : %v\n", err) 111 | os.Exit(1) 112 | } 113 | 114 | printer.Output(servers) 115 | }, 116 | } 117 | 118 | var serverDestroy = &cobra.Command{ 119 | Use: "destroy", 120 | Short: "Permanently delete a server", 121 | Long: `destroy `, 122 | Aliases: []string{"delete", "d", "del", "rm"}, 123 | Args: func(cmd *cobra.Command, args []string) error { 124 | if len(args) < 1 { 125 | return errors.New("please provide a server ID") 126 | } 127 | return nil 128 | }, 129 | Run: func(cmd *cobra.Command, args []string) { 130 | id := args[0] 131 | err := client.Server.Destroy(id) 132 | if err != nil { 133 | fmt.Printf("Error destroying server : %v\n", err) 134 | os.Exit(1) 135 | } 136 | 137 | fmt.Println("Deleted server") 138 | }, 139 | } 140 | 141 | var serverCreate = &cobra.Command{ 142 | Use: "create", 143 | Short: "Create a new server", 144 | Long: ``, 145 | Aliases: []string{"c"}, 146 | Run: func(cmd *cobra.Command, args []string) { 147 | opts := gobitlaunch.CreateServerOptions{} 148 | opts.Name, _ = cmd.Flags().GetString("name") 149 | host, _ := cmd.Flags().GetString("host") 150 | opts.HostImageID, _ = cmd.Flags().GetString("image") 151 | opts.SizeID, _ = cmd.Flags().GetString("size") 152 | opts.RegionID, _ = cmd.Flags().GetString("region") 153 | opts.SSHKeys, _ = cmd.Flags().GetStringSlice("sshkey") 154 | opts.Password, _ = cmd.Flags().GetString("password") 155 | opts.InitScript, _ = cmd.Flags().GetString("initscript") 156 | 157 | // validate 158 | if len(opts.Password) == 0 && len(opts.SSHKeys) == 0 { 159 | fmt.Println("You must provide either --sshkey or --password") 160 | os.Exit(1) 161 | } 162 | 163 | var err error 164 | opts.HostID, err = hostID(host) 165 | if err != nil { 166 | fmt.Println(err) 167 | os.Exit(1) 168 | } 169 | 170 | server, err := client.Server.Create(&opts) 171 | if err != nil { 172 | fmt.Printf("Error creating server : %v\n", err) 173 | os.Exit(1) 174 | } 175 | 176 | printer.Output(server) 177 | }, 178 | } 179 | 180 | var serverRebuild = &cobra.Command{ 181 | Use: "rebuild", 182 | Short: "Rebuild a server", 183 | Long: `rebuild `, 184 | Aliases: []string{}, 185 | Args: func(cmd *cobra.Command, args []string) error { 186 | if len(args) < 1 { 187 | return errors.New("please provide a server ID") 188 | } 189 | return nil 190 | }, 191 | Run: func(cmd *cobra.Command, args []string) { 192 | id := args[0] 193 | opts := gobitlaunch.RebuildOptions{} 194 | opts.ID, _ = cmd.Flags().GetString("image") 195 | opts.Description, _ = cmd.Flags().GetString("description") 196 | 197 | err := client.Server.Rebuild(id, &opts) 198 | if err != nil { 199 | fmt.Printf("Error rebuilding server : %v\n", err) 200 | os.Exit(1) 201 | } 202 | 203 | fmt.Println("Rebuilding server") 204 | }, 205 | } 206 | 207 | var serverResize = &cobra.Command{ 208 | Use: "resize", 209 | Short: "Resize a server", 210 | Long: `resize `, 211 | Aliases: []string{}, 212 | Args: func(cmd *cobra.Command, args []string) error { 213 | if len(args) < 1 { 214 | return errors.New("please provide a server ID") 215 | } 216 | return nil 217 | }, 218 | Run: func(cmd *cobra.Command, args []string) { 219 | id := args[0] 220 | sizeID, _ := cmd.Flags().GetString("size") 221 | 222 | err := client.Server.Resize(id, sizeID) 223 | if err != nil { 224 | fmt.Printf("Error resizing server : %v\n", err) 225 | os.Exit(1) 226 | } 227 | 228 | fmt.Println("Resizing server") 229 | }, 230 | } 231 | 232 | var serverRestart = &cobra.Command{ 233 | Use: "restart", 234 | Short: "Restart a server", 235 | Long: `restart `, 236 | Aliases: []string{"reboot"}, 237 | Args: func(cmd *cobra.Command, args []string) error { 238 | if len(args) < 1 { 239 | return errors.New("please provide a server ID") 240 | } 241 | return nil 242 | }, 243 | Run: func(cmd *cobra.Command, args []string) { 244 | id := args[0] 245 | err := client.Server.Restart(id) 246 | if err != nil { 247 | fmt.Printf("Error restarting server : %v\n", err) 248 | os.Exit(1) 249 | } 250 | 251 | fmt.Println("Restarted server") 252 | }, 253 | } 254 | 255 | var serverProtection = &cobra.Command{ 256 | Use: "protection", 257 | Short: "Protect a server", 258 | Long: `protection [enable true e] or [disable false d]`, 259 | Aliases: []string{"protect"}, 260 | Args: func(cmd *cobra.Command, args []string) error { 261 | if len(args) < 1 { 262 | return errors.New("please provide a server ID") 263 | } 264 | if len(args) < 2 { 265 | return errors.New("please provide a protection state") 266 | } 267 | return nil 268 | }, 269 | Run: func(cmd *cobra.Command, args []string) { 270 | id := args[0] 271 | 272 | server, err := client.Server.Protection(id, func() bool { 273 | if args[1] == "enable" || args[1] == "true" || args[1] == "e" { 274 | return true 275 | } else if args[1] == "disable" || args[1] == "false" || args[1] == "d" { 276 | return false 277 | } 278 | 279 | fmt.Println("Invalid protection state") 280 | os.Exit(1) 281 | return false 282 | }()) 283 | if err != nil { 284 | fmt.Printf("Error resizing server : %v\n", err) 285 | os.Exit(1) 286 | } 287 | 288 | printer.Output(server) 289 | }, 290 | } 291 | 292 | var serverSetPorts = &cobra.Command{ 293 | Use: "setports", 294 | Short: "Set ports for a protected server", 295 | Long: `setports `, 296 | Aliases: []string{"ports"}, 297 | Args: func(cmd *cobra.Command, args []string) error { 298 | if len(args) < 1 { 299 | return errors.New("please provide a server ID") 300 | } 301 | return nil 302 | }, 303 | Run: func(cmd *cobra.Command, args []string) { 304 | id := args[0] 305 | ports, _ := cmd.Flags().GetString("ports") 306 | 307 | portItems := strings.Split(ports, ",") 308 | portList := []gobitlaunch.Ports{} 309 | 310 | for _, port := range portItems { 311 | portObj := strings.Split(port, ":") 312 | 313 | num, err := strconv.Atoi(portObj[0]) 314 | if err != nil { 315 | fmt.Printf("Error setting server ports : %v\n", err) 316 | os.Exit(1) 317 | } 318 | 319 | portList = append(portList, gobitlaunch.Ports{ 320 | PortNumber: num, 321 | Protocol: portObj[1], 322 | }) 323 | } 324 | 325 | server, err := client.Server.SetPorts(id, &portList) 326 | if err != nil { 327 | fmt.Printf("Error setting server ports : %v\n", err) 328 | os.Exit(1) 329 | } 330 | 331 | printer.Output(server) 332 | }, 333 | } 334 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | https://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | Copyright 2015 Bryan Liles 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | https://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. --------------------------------------------------------------------------------