-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.go
40 lines (33 loc) · 999 Bytes
/
autocomplete.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
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2021 Roberto Hidalgo <milpa@un.rob.mx>
package command
import (
"fmt"
"os"
"strings"
"time"
"git.rob.mx/nidito/chinampa/pkg/command"
"git.rob.mx/nidito/chinampa/pkg/exec"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func MilpaComplete(cmd *command.Command, currentValue string, config string) (values []string, flag cobra.ShellCompDirective, err error) {
cmdLine, err := cmd.ResolveTemplate(config, currentValue)
if err != nil {
return nil, cobra.ShellCompDirectiveError, err
}
args := append([]string{"milpa"}, strings.Split(cmdLine, " ")...)
envMap := EnvironmentMap(cmd)
env := os.Environ()
for k, v := range envMap {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
values, flag, err = exec.Exec(cmd.FullName(), args, env, 5*time.Second, logrus.WithContext(cmd.Cobra.Context()))
if err != nil {
return nil, flag, err
}
return
}
func init() {
command.RegisterValueSource("milpa", MilpaComplete)
}