|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/urfave/cli/v2" |
| 5 | + "github.com/chermehdi/egor/config" |
| 6 | + "github.com/fatih/color" |
| 7 | + "github.com/atotto/clipboard" |
| 8 | + "os" |
| 9 | + "path" |
| 10 | + "io/ioutil" |
| 11 | +) |
| 12 | + |
| 13 | +// Load the content of a given file |
| 14 | +func GetFileContent(filePath string) (string, error) { |
| 15 | + filebytes, err := ioutil.ReadFile(filePath) |
| 16 | + if err != nil { |
| 17 | + return "", err |
| 18 | + } |
| 19 | + |
| 20 | + filecontent := string(filebytes) |
| 21 | + return filecontent, nil |
| 22 | +} |
| 23 | + |
| 24 | +func CopyAction(context *cli.Context) error { |
| 25 | + cwd, err := os.Getwd() |
| 26 | + if err != nil { |
| 27 | + color.Red("Failed to list test cases!") |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + configuration, err := config.LoadDefaultConfiguration() |
| 32 | + if err != nil { |
| 33 | + color.Red("Failed to load egor configuration") |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + configFileName := configuration.ConfigFileName |
| 38 | + metaData, err := config.LoadMetaFromPath(path.Join(cwd, configFileName)) |
| 39 | + if err != nil { |
| 40 | + color.Red("Failed to load egor MetaData ") |
| 41 | + return err |
| 42 | + } |
| 43 | + |
| 44 | + taskFile := metaData.TaskFile |
| 45 | + taskContent, err := GetFileContent(taskFile) |
| 46 | + if err != nil { |
| 47 | + color.Red("Failed to load task file content") |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + err = clipboard.WriteAll(taskContent) |
| 52 | + if err != nil { |
| 53 | + color.Red("Failed to copy task content to clipboard") |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + color.Green("Done!") |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +// Command to copy task source file to clipboard for easy submit. |
| 62 | +// Running this command will fetch egor meta data, get the content of the task source |
| 63 | +// and then copy the content to the clipboard. |
| 64 | +var CopyCommand = cli.Command{ |
| 65 | + Name: "showcases", |
| 66 | + Aliases: []string{"cp"}, |
| 67 | + Usage: "copy task file into clipboad", |
| 68 | + UsageText: "list meta data about of the tests cases in the current task", |
| 69 | + Action: CopyAction, |
| 70 | +} |
0 commit comments