Skip to content

Commit

Permalink
Alias system
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatGodApollo committed Apr 2, 2020
1 parent 6ea9b02 commit bcc787c
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 32 deletions.
35 changes: 35 additions & 0 deletions cmd/gists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Quick Script Runner: A quick and easy way to run gists
* Copyright © 2020 Brett Bender
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package cmd

import "github.com/spf13/viper"

func SetGists() {
/* Repository of different user's gists!
* If you'd like your personal gist here, create a pull request, adding it to this list,
* and to gists.md
*/


//// "apollo": GreatGodApollo (Author)
viper.Set("apollo", map[string]string{"gist": "00d91bfb540b8bc0169606b4d4f740a3"})

//// "324luke": 324Luke (Contributor)
viper.Set("324luke", map[string]string{"gist": "3a30e079e8692cc09776186014090835"})
}
4 changes: 2 additions & 2 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func DownloadFile(filepath string, url string) error {
func CheckError(err error) bool {
if err != nil {
if strings.Contains(err.Error(), "executable file not found") {
fmt.Println(chalk.Cyan.Color("[QSR]"), chalk.Red.Color("That language isn't supported on your system!"))
fmt.Println(NewMessage(chalk.Red, "That language isn't supported on your system!").Build())
return true
}
fmt.Println(chalk.Cyan.Color("[QSR]"), chalk.Red.Color(err.Error()))
fmt.Println(NewMessage(chalk.Red, err.Error()).Build())
return true
}
return false
Expand Down
56 changes: 56 additions & 0 deletions cmd/link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
)

// createCmd represents the create command
var linkCmd = &cobra.Command{
Use: "link <alias> <gist> [file]",
Short: "Link a gist or file alias",
Long: `Link a gist or file alias in your config.`,
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 2 {
viper.Set(args[0], map[string]string{"gist": args[1], "file": ""})
err := viper.WriteConfig()
if CheckError(err) {return}
fmt.Println(NewMessage(chalk.Green, "Successfully wrote gist alias").
ThenColor(chalk.Cyan, args[0]).
ThenColor(chalk.Green, "as").
ThenColor(chalk.Cyan, args[1]).Build())
} else {
viper.Set(args[0], map[string]string{"gist": args[1], "file": args[2]})
err := viper.WriteConfig()
if CheckError(err) {return}
fmt.Println(NewMessage(chalk.Green, "Successfully wrote gist alias").
ThenColor(chalk.Cyan, args[0]).
ThenColor(chalk.Green, "as").
ThenColor(chalk.Cyan, args[1]).
ThenColor(chalk.Green,"with file").
ThenColor(chalk.Cyan, args[2]).Build())
}
},
}

func init() {
rootCmd.AddCommand(linkCmd)
}
51 changes: 51 additions & 0 deletions cmd/messageBuilder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Quick Script Runner: A quick and easy way to run gists
* Copyright © 2020 Brett Bender
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package cmd

import "github.com/ttacon/chalk"

type Message struct {
message string
}

func NewMessage(color chalk.Color, message string) *Message {
return &Message{
message: chalk.Cyan.Color("[QSR] ") + color.Color(message),
}
}

func (msg *Message) ThenColor(color chalk.Color, message string) *Message {
msg.message = msg.message + " " + color.Color(message)
return msg
}

func (msg *Message) ThenStyle(style chalk.TextStyle, message string) *Message {
msg.message = msg.message + " " + style.TextStyle(message)
return msg
}

func (msg *Message) ThenColorStyle(color chalk.Color, style chalk.TextStyle, message string) *Message {
msg.message = msg.message + " " + color.Color(style.TextStyle(message))
return msg
}

func (msg *Message) Build() string {
return msg.message
}

49 changes: 49 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ package cmd

import (
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
"os"
"strings"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "qsr [command] [arguments]",
Expand All @@ -36,10 +42,53 @@ with a single command.`,
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
if strings.HasPrefix(err.Error(), "unknown command") {
return
}
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.qsr.json)")
}

func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := homedir.Dir()
if CheckError(err) {os.Exit(1)}

viper.AddConfigPath(home)
viper.SetConfigName(".qsr")
viper.SetConfigType("json")
if createFileIfNotExist(home + "/", ".qsr.json") != nil {os.Exit(1)}

}

viper.AutomaticEnv()

if err := viper.ReadInConfig(); err == nil {
fmt.Println(NewMessage(chalk.Blue, "Using config file:").ThenColor(chalk.Green, viper.ConfigFileUsed()).Build())
}
SetGists()
if CheckError(viper.WriteConfig()) {os.Exit(1)}
}

func createFileIfNotExist(dir, file string) error {
if _, err := os.Stat(dir + file); err != nil {
if os.IsNotExist(err) {
f, err := os.Create(dir + file)
if CheckError(err) {return err}
err = f.Close()
if CheckError(err) {return err}
} else {
return err
}
}
return nil
}
Loading

0 comments on commit bcc787c

Please sign in to comment.