-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
62 lines (48 loc) · 1.52 KB
/
main.go
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
// Copyright (C) 2019 Vanessa Sochat.
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"fmt"
nu "github.com/vsoch/nu-plugin/pkg/plugin"
)
// sink is required for you to implement to parse parameters and print
// whatever you like to stdout! It should take the plugin object passed,
// along with params from nushell. You are responsible
// for parsing the params, and can use plugin.Func.GetNamedParams(params)
func sink(plugin *nu.SinkPlugin, params interface{}) {
// a map[string]interface{} with keys, values
namedParams := plugin.Func.GetNamedParams(params)
message := "Hello"
excited := false
// First pass we look for booleans
for name, _ := range namedParams {
switch name {
case "excited":
excited = true
}
}
// Second pass, look for string args
for name, value := range namedParams {
if name == "name" {
message = message + " " + value.(string)
}
}
// Add an exclamation point?
if excited {
message = message + "!"
}
fmt.Println(message)
}
func main() {
name := "hello"
usage := "A friendly plugin"
plugin := nu.NewSinkPlugin(name, usage)
// Add named arguments: key, config.SyntaxShape, config.ArgType
// Switch should be provided with empty string
plugin.Config.AddNamedParam("excited", "Switch", "")
plugin.Config.AddNamedParam("name", "Optional", "String")
// Run the filter function
plugin.Run(sink)
}