Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-cmds
Commits
b86d2a8b
Commit
b86d2a8b
authored
Oct 07, 2017
by
keks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add examples for Encoders and PostRun
parent
bf9328e2
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
153 additions
and
12 deletions
+153
-12
.gitignore
.gitignore
+3
-0
examples/adder/cmd.go
examples/adder/cmd.go
+123
-3
examples/adder/local/main.go
examples/adder/local/main.go
+15
-2
examples/adder/remote/client/main.go
examples/adder/remote/client/main.go
+10
-5
examples/adder/remote/server/main.go
examples/adder/remote/server/main.go
+2
-2
No files found.
.gitignore
View file @
b86d2a8b
.*.swp
.*.swo
cover.out
examples/adder/local/local
examples/adder/remote/client/client
examples/adder/remote/server/server
examples/adder/cmd.go
View file @
b86d2a8b
...
...
@@ -2,16 +2,29 @@ package adder
import
(
"fmt"
"io"
"strconv"
"strings"
"time"
"g
ithub.com/ipfs
/go-ipfs-cmdkit"
"gx/ipfs/Qm
ezbW7VUAiu3aSV6r4TdB9pwficnnbtWYKRsoEKF2w8G2
/go-ipfs-cmds"
"g
x/ipfs/QmPMeikDc7tQEDvaS66j1bVPQ2jBkvFwz3Qom5eA5i4xip
/go-ipfs-cmdkit"
"gx/ipfs/Qm
PhtZyjPYddJ8yGPWreisp47H6iQjt3Lg8sZrzqMP5noy
/go-ipfs-cmds"
)
// AddStatus describes the progress of the add operation
type
AddStatus
struct
{
// Current is the current value of the sum.
Current
int
// Left is how many summands are left
Left
int
}
// Define the root of the commands
var
RootCmd
=
&
cmds
.
Command
{
Subcommands
:
map
[
string
]
*
cmds
.
Command
{
"add"
:
&
cmds
.
Command
{
// the simplest way to make an adder
"simpleAdd"
:
&
cmds
.
Command
{
Arguments
:
[]
cmdkit
.
Argument
{
cmdkit
.
StringArg
(
"summands"
,
true
,
true
,
"values that are supposed to be summed"
),
},
...
...
@@ -32,5 +45,112 @@ var RootCmd = &cmds.Command{
re
.
Emit
(
fmt
.
Sprintf
(
"total: %d"
,
sum
))
},
},
// a bit more sophisticated
"encodeAdd"
:
&
cmds
.
Command
{
Arguments
:
[]
cmdkit
.
Argument
{
cmdkit
.
StringArg
(
"summands"
,
true
,
true
,
"values that are supposed to be summed"
),
},
Run
:
func
(
req
cmds
.
Request
,
re
cmds
.
ResponseEmitter
)
{
sum
:=
0
for
i
,
str
:=
range
req
.
Arguments
()
{
num
,
err
:=
strconv
.
Atoi
(
str
)
if
err
!=
nil
{
re
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
sum
+=
num
re
.
Emit
(
&
AddStatus
{
Current
:
sum
,
Left
:
len
(
req
.
Arguments
())
-
i
-
1
,
})
time
.
Sleep
(
200
*
time
.
Millisecond
)
}
},
Type
:
&
AddStatus
{},
Encoders
:
cmds
.
EncoderMap
{
// This defines how to encode these values as text. Other possible encodings are XML and JSON.
cmds
.
Text
:
cmds
.
MakeEncoder
(
func
(
req
cmds
.
Request
,
w
io
.
Writer
,
v
interface
{})
error
{
s
,
ok
:=
v
.
(
*
AddStatus
)
if
!
ok
{
return
fmt
.
Errorf
(
"cast error, got type %T"
,
v
)
}
if
s
.
Left
==
0
{
fmt
.
Fprintln
(
w
,
"total:"
,
s
.
Current
)
}
else
{
fmt
.
Fprintf
(
w
,
"intermediate result: %d; %d left
\n
"
,
s
.
Current
,
s
.
Left
)
}
return
nil
}),
},
},
// the best UX
"postRunAdd"
:
&
cmds
.
Command
{
Arguments
:
[]
cmdkit
.
Argument
{
cmdkit
.
StringArg
(
"summands"
,
true
,
true
,
"values that are supposed to be summed"
),
},
// this is the same as for encoderAdd
Run
:
func
(
req
cmds
.
Request
,
re
cmds
.
ResponseEmitter
)
{
sum
:=
0
for
i
,
str
:=
range
req
.
Arguments
()
{
num
,
err
:=
strconv
.
Atoi
(
str
)
if
err
!=
nil
{
re
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
sum
+=
num
re
.
Emit
(
&
AddStatus
{
Current
:
sum
,
Left
:
len
(
req
.
Arguments
())
-
i
-
1
,
})
time
.
Sleep
(
200
*
time
.
Millisecond
)
}
},
Type
:
&
AddStatus
{},
PostRun
:
cmds
.
PostRunMap
{
cmds
.
CLI
:
func
(
req
cmds
.
Request
,
re
cmds
.
ResponseEmitter
)
cmds
.
ResponseEmitter
{
reNext
,
res
:=
cmds
.
NewChanResponsePair
(
req
)
go
func
()
{
defer
re
.
Close
()
defer
fmt
.
Println
()
// length of line at last iteration
var
lastLen
int
for
{
v
,
err
:=
res
.
Next
()
if
err
==
io
.
EOF
{
return
}
if
err
==
cmds
.
ErrRcvdError
{
fmt
.
Println
(
"
\n
received error:"
,
res
.
Error
())
return
}
if
err
!=
nil
{
fmt
.
Println
(
"
\n
error:"
,
err
)
return
}
fmt
.
Print
(
"
\r
"
+
strings
.
Repeat
(
" "
,
lastLen
))
s
:=
v
.
(
*
AddStatus
)
if
s
.
Left
>
0
{
lastLen
,
_
=
fmt
.
Printf
(
"
\r
calculation sum... current: %d; left: %d"
,
s
.
Current
,
s
.
Left
)
}
else
{
lastLen
,
_
=
fmt
.
Printf
(
"
\r
sum is %d."
,
s
.
Current
)
}
}
}()
return
reNext
},
},
},
},
}
examples/adder/local/main.go
View file @
b86d2a8b
...
...
@@ -5,7 +5,8 @@ import (
"github.com/ipfs/go-ipfs-cmds/examples/adder"
"gx/ipfs/QmezbW7VUAiu3aSV6r4TdB9pwficnnbtWYKRsoEKF2w8G2/go-ipfs-cmds/cli"
"gx/ipfs/QmPhtZyjPYddJ8yGPWreisp47H6iQjt3Lg8sZrzqMP5noy/go-ipfs-cmds/cli"
"gx/ipfs/QmPhtZyjPYddJ8yGPWreisp47H6iQjt3Lg8sZrzqMP5noy/go-ipfs-cmds"
)
func
main
()
{
...
...
@@ -14,12 +15,21 @@ func main() {
if
err
!=
nil
{
panic
(
err
)
}
req
.
SetOption
(
"encoding"
,
cmds
.
Text
)
// create an emitter
re
,
retCh
:=
cli
.
NewResponseEmitter
(
os
.
Stdout
,
os
.
Stderr
,
cmd
.
Encoders
[
"Text"
],
req
)
if
pr
,
ok
:=
cmd
.
PostRun
[
cmds
.
CLI
];
ok
{
re
=
pr
(
req
,
re
)
}
wait
:=
make
(
chan
struct
{})
// call command in background
go
func
()
{
defer
close
(
wait
)
err
=
adder
.
RootCmd
.
Call
(
req
,
re
)
if
err
!=
nil
{
panic
(
err
)
...
...
@@ -27,5 +37,8 @@ func main() {
}()
// wait until command has returned and exit
os
.
Exit
(
<-
retCh
)
ret
:=
<-
retCh
<-
wait
os
.
Exit
(
ret
)
}
examples/adder/remote/client/main.go
View file @
b86d2a8b
...
...
@@ -5,11 +5,10 @@ import (
"github.com/ipfs/go-ipfs-cmds/examples/adder"
cmdkit
"github.com/ipfs/go-ipfs-cmdkit"
cmds
"gx/ipfs/QmezbW7VUAiu3aSV6r4TdB9pwficnnbtWYKRsoEKF2w8G2/go-ipfs-cmds"
cli
"gx/ipfs/QmezbW7VUAiu3aSV6r4TdB9pwficnnbtWYKRsoEKF2w8G2/go-ipfs-cmds/cli"
http
"gx/ipfs/QmezbW7VUAiu3aSV6r4TdB9pwficnnbtWYKRsoEKF2w8G2/go-ipfs-cmds/http"
cmdkit
"gx/ipfs/QmPMeikDc7tQEDvaS66j1bVPQ2jBkvFwz3Qom5eA5i4xip/go-ipfs-cmdkit"
cmds
"gx/ipfs/QmPhtZyjPYddJ8yGPWreisp47H6iQjt3Lg8sZrzqMP5noy/go-ipfs-cmds"
cli
"gx/ipfs/QmPhtZyjPYddJ8yGPWreisp47H6iQjt3Lg8sZrzqMP5noy/go-ipfs-cmds/cli"
http
"gx/ipfs/QmPhtZyjPYddJ8yGPWreisp47H6iQjt3Lg8sZrzqMP5noy/go-ipfs-cmds/http"
)
func
main
()
{
...
...
@@ -27,10 +26,16 @@ func main() {
if
err
!=
nil
{
panic
(
err
)
}
req
.
SetOption
(
"encoding"
,
cmds
.
Text
)
// create an emitter
re
,
retCh
:=
cli
.
NewResponseEmitter
(
os
.
Stdout
,
os
.
Stderr
,
cmd
.
Encoders
[
"Text"
],
req
)
if
pr
,
ok
:=
cmd
.
PostRun
[
cmds
.
CLI
];
ok
{
re
=
pr
(
req
,
re
)
}
wait
:=
make
(
chan
struct
{})
// copy received result into cli emitter
go
func
()
{
...
...
examples/adder/remote/server/main.go
View file @
b86d2a8b
...
...
@@ -7,8 +7,8 @@ import (
"github.com/ipfs/go-ipfs-cmds/examples/adder"
cmds
"gx/ipfs/Qm
ezbW7VUAiu3aSV6r4TdB9pwficnnbtWYKRsoEKF2w8G2
/go-ipfs-cmds"
http
"gx/ipfs/Qm
ezbW7VUAiu3aSV6r4TdB9pwficnnbtWYKRsoEKF2w8G2
/go-ipfs-cmds/http"
cmds
"gx/ipfs/Qm
PhtZyjPYddJ8yGPWreisp47H6iQjt3Lg8sZrzqMP5noy
/go-ipfs-cmds"
http
"gx/ipfs/Qm
PhtZyjPYddJ8yGPWreisp47H6iQjt3Lg8sZrzqMP5noy
/go-ipfs-cmds/http"
)
func
main
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment