Skip to content

Commit 9c88a51

Browse files
authored
Merge pull request #20 from chermehdi/feature/copy
Implement copy to clipboard
2 parents 76f83fa + 635d8f3 commit 9c88a51

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

commands/copy.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
"fmt"
12+
)
13+
14+
// Load the content of a given file
15+
func GetFileContent(filePath string) (string, error) {
16+
filebytes, err := ioutil.ReadFile(filePath)
17+
if err != nil {
18+
return "", err
19+
}
20+
21+
filecontent := string(filebytes)
22+
return filecontent, nil
23+
}
24+
25+
func CopyAction(context *cli.Context) error {
26+
cwd, err := os.Getwd()
27+
if err != nil {
28+
color.Red(fmt.Sprintf("Failed to list test cases : %s", err.Error()))
29+
return err
30+
}
31+
32+
configuration, err := config.LoadDefaultConfiguration()
33+
if err != nil {
34+
color.Red(fmt.Sprintf("Failed to load egor configuration: %s", err.Error()))
35+
return err
36+
}
37+
38+
configFileName := configuration.ConfigFileName
39+
metaData, err := config.LoadMetaFromPath(path.Join(cwd, configFileName))
40+
if err != nil {
41+
color.Red(fmt.Sprintf("Failed to load egor MetaData : %s", err.Error()))
42+
return err
43+
}
44+
45+
taskFile := metaData.TaskFile
46+
taskContent, err := GetFileContent(taskFile)
47+
if err != nil {
48+
color.Red(fmt.Sprintf("Failed to load task file content : %s", err.Error()))
49+
return err
50+
}
51+
52+
err = clipboard.WriteAll(taskContent)
53+
if err != nil {
54+
color.Red(fmt.Sprintf("Failed to copy task content to clipboard : %s", err.Error()))
55+
return err
56+
}
57+
58+
color.Green("Task copied to clipboard successfully")
59+
return nil
60+
}
61+
62+
// Command to copy task source file to clipboard for easy submit.
63+
// Running this command will fetch egor meta data, get the content of the task source
64+
// and then copy the content to the clipboard.
65+
var CopyCommand = cli.Command{
66+
Name: "copy",
67+
Aliases: []string{"cp"},
68+
Usage: "copy task file into clipboad",
69+
UsageText: "list meta data about of the tests cases in the current task",
70+
Action: CopyAction,
71+
}

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func main() {
4545
&commands.ConfigCommand,
4646
&commands.CaseCommand,
4747
&commands.ShowCasesCommand,
48+
&commands.CopyCommand,
4849
&commands.PrintCaseCommand,
4950
},
5051
}

0 commit comments

Comments
 (0)