-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_basecommand_simple_test.go
More file actions
145 lines (121 loc) · 3.38 KB
/
example_basecommand_simple_test.go
File metadata and controls
145 lines (121 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package cmder_test
import (
"archive/tar"
"bytes"
"context"
"encoding/hex"
"flag"
"fmt"
"io"
"os"
"github.com/brandon1024/cmder"
)
// This example demonstrates the simplest usage of [cmder.BaseCommand]. By using BaseCommand directly, you don't need to
// define your own command types. This can be a nice convenience for simple commands.
func ExampleBaseCommand() {
exampleSetup()
args := []string{"-Co-", "-"}
if err := cmder.Execute(context.Background(), untar, cmder.WithArgs(args)); err != nil {
fmt.Printf("unexpected error occurred: %v", err)
}
// Output:
// 00000000 0a 27 75 6e 74 61 72 27 20 64 65 6d 6f 6e 73 74 |.'untar' demonst|
// 00000010 72 61 74 65 73 20 74 68 65 20 73 69 6d 70 6c 65 |rates the simple|
// 00000020 73 74 20 75 73 61 67 65 20 6f 66 20 5b 63 6d 64 |st usage of [cmd|
// 00000030 65 72 2e 42 61 73 65 43 6f 6d 6d 61 6e 64 5d 2c |er.BaseCommand],|
// 00000040 20 75 73 69 6e 67 20 74 68 65 20 74 79 70 65 20 | using the type |
// 00000050 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 73 |to implement a s|
// 00000060 69 6d 70 6c 65 20 63 6f 6d 6d 61 6e 64 20 74 68 |imple command th|
// 00000070 61 74 20 72 65 61 64 73 0a 61 20 74 61 72 20 61 |at reads.a tar a|
// 00000080 72 63 68 69 76 65 20 61 6e 64 20 64 75 6d 70 73 |rchive and dumps|
// 00000090 20 69 6e 66 6c 61 74 65 64 20 63 6f 6e 74 65 6e | inflated conten|
// 000000a0 74 2e 0a |t..|
}
const UntarDesc = `
'untar' demonstrates the simplest usage of [cmder.BaseCommand], using the type to implement a simple command that reads
a tar archive and dumps inflated content.
`
const UntarExamples = `
untar example.tar
untar -Co=example.out example.tar
untar -Co- - <example.tar
`
var (
untar = &cmder.BaseCommand{
CommandName: "untar",
CommandDocumentation: cmder.CommandDocumentation{
Usage: "untar [-o <file>] [-C] <file>",
ShortHelp: "A simple demonstration of direct usage of BaseCommand.",
Help: UntarDesc,
Examples: UntarExamples,
},
RunFunc: run,
InitFlagsFunc: flags,
}
)
var (
output string = "-"
hexdump bool
)
var (
in io.ReadWriter = os.Stdin
)
// Configure flags. We register two flags, '-o' and '-C'.
func flags(fs *flag.FlagSet) {
fs.StringVar(&output, "o", output, "dump archive content to `file`")
fs.BoolVar(&hexdump, "C", hexdump, "dump archive content in a canonical hex+ascii format")
}
// The command's run function.
func run(ctx context.Context, args []string) error {
if len(args) != 1 {
return cmder.ErrShowUsage
}
var out io.Writer = os.Stdout
if args[0] != "-" {
inputFile, err := os.Open(args[0])
if err != nil {
return err
}
defer inputFile.Close()
in = inputFile
}
if output != "-" {
outputFile, err := os.Create(output)
if err != nil {
return err
}
defer outputFile.Close()
out = outputFile
}
if hexdump {
dumper := hex.Dumper(out)
defer dumper.Close()
out = dumper
}
reader := tar.NewReader(in)
for {
_, err := reader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if _, err := io.Copy(out, reader); err != nil {
return err
}
}
return nil
}
// Setup for the example.
func exampleSetup() {
in = &bytes.Buffer{}
tw := tar.NewWriter(in)
defer tw.Close()
tw.WriteHeader(&tar.Header{
Name: "usage-example",
Mode: 0644,
Size: int64(len(UntarDesc)),
})
tw.Write([]byte(UntarDesc))
}