dongyin2643 2019-05-03 21:38
浏览 43

在go cli函数中如何接受文本字符串?

I'm trying to work on a PoC with adding exec.Command's to cli plugin, however I'm struggling with how to get it to accept user input with the command. I admit I am fairly new to Go, coming from PHP and Bash scripting so...I am sure I'm missing something very basic here and it's a little embarrassing.

I've tried setting variables, adding the variables to the output, adding in bufio, and trying to prompt with no luck.

out, err := exec.Command("mtr", "-c", "10", "-r", destIP).Output()

was the most recent fail...

type Mtr struct{}

func (m *Mtr) Run(command []string, context plugin.PluginContext, ui terminal.UI) {
    // var destIP string - commented out simply so I could rebuild the file and get the plugin working again.

    ui.Say("")
    ui.Say(terminal.AdvisoryColor("Wait while we run a traceroute..."))
    ui.Say("")

    out, err := exec.Command("mtr", "-c", "10", "-r").Output()
    if err != nil {
        ui.Say(fmt.Sprintf("%s", err))
    }
    output := string(out[:])

    table := ui.Table([]string{"", ""})

    table.Add("", output)
    table.Print()

I literally just want it to be able to accept something like this

mainprogram plugin mtr Currently I have it set to just force Google, but that is not really what I want out of this...

out, err := exec.Command("mtr", "-c", "10", "-r", "8.8.8.8").Output()

Current output with the botched attempts yields -

Shawns-MBP-2:directoryhere $ mainprog plugin mtr 4.4.4.4

Wait while we run a traceroute...



Shawns-MBP-2:directoryhere$

With the forced Google DNS, it obviously shows -

Shawns-MBP-2:directoryhere $ mainprog plugin mtr

Wait while we run a traceroute...


   Start: 2019-05-03T15:41:18-0500   
   HOST: stuff   Loss%   Snt   Last   Avg  Best  Wrst StDev   
     1.|-- stuff   0.0%    10    1.6   1.7   1.2   3.1   0.5   
     2.|-- stuff  0.0%    10    2.6   6.5   2.0  12.4   4.5   
     3.|-- stuff               0.0%    10    3.3   3.3   2.7   4.1   0.4   
     4.|-- stuff       0.0%    10   13.6  12.2   9.1  15.6   2.5   
     5.|-- stuff               0.0%    10    9.2  12.9   9.2  17.7   2.5   
     6.|-- stuff              0.0%    10    8.7   9.1   8.0  10.5   0.6   
     7.|-- stuff               0.0%    10    9.1   9.2   8.2  10.1   0.5   
     8.|-- stuff            0.0%    10   10.3  10.1   9.7  10.4   0.3   
     9.|-- stuff              0.0%    10    8.8   9.2   8.5  10.7   0.6   
    10.|-- google-public-dns-a.googl  0.0%    10    8.7   8.9   8.2  10.0   0.4
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c