【问题标题】:exec.Command().Run() not executing command properly in Windowsexec.Command().Run() 未在 Windows 中正确执行命令
【发布时间】:2021-01-18 02:20:01
【问题描述】:

我有一个名为 config-lint: https://github.com/stelligent/config-lint 的 linting 实用程序,我为 Windows 下载了它。

我下载了它并将其添加到路径中并能够运行它,如下所示

E:\>config-lint -terraform config-lint-master\example-files\config [   {
    "AssertionMessage": "None expression fails: ",
    "Category": "resource",
    "CreatedAt": "2020-10-02T10:23:19Z",
    "Filename": "config-lint-master\\example-files\\config\\s3.tf",
    "LineNumber": 43,
    "ResourceID": "bucket_with_not",
    "ResourceType": "aws_s3_bucket_policy",
    "RuleID": "S3_NOT_ACTION",
    "RuleMessage": "Should not use NotAction in S3 bucket policy",
    "Status": "WARNING"   },   {
    "AssertionMessage": "Not expression fails",
    "Category": "resource",
    "CreatedAt": "2020-10-02T10:23:19Z",
    "Filename": "config-lint-master\\example-files\\config\\security_group.tf",
    "LineNumber": 1,
    "ResourceID": "allow_all",
    "ResourceType": "aws_security_group",
    "RuleID": "SG_INGRESS_ALL_PROTOCOLS",
    "RuleMessage": "Best practices recommend not opening all protocols and ports to ingress traffic",
    "Status": "WARNING"   },

--- 更多输出跟随

我希望通过 go Lang 中的 exec 包实现相同的目的。下面是我的代码

 args := []string{"-terraform", "E:\\config-lint-master\\example-files\\config"}
    app := "config-lint"
    cmd := exec.Command(app, args...)
    //cmd := exec.Command("config-lint", "-terraform", "E:\\config-lint-master\\example-files\\config")
    cmd.Stdout = &bytes.Buffer{}
    cmdOp := &bytes.Buffer{}
    cmd.Stdout = cmdOp
    err := cmd.Run()
    if err != nil {
        fmt.Println("Error->", err)
    } else {
        fmt.Println(cmdOp)
    }

执行此操作时,我收到错误

错误-> 退出状态4294967295

我不知道这意味着什么,我是 Go Lang 的新手。

注意:当我通过在末尾传递一个文件名使用下面的命令运行它时,它就会执行。传递目录时似乎不起作用。

 args := []string{"-terraform", "E:\\config-lint-master\\example-files\\config\\elb.tf"}

config-lint 可以如下运行

config-lint -terraform

参考:https://stelligent.github.io/config-lint/#/terraform

Go Lang 版本:go 版本 go1.14.6 windows/amd64

操作系统:Win 10 Home,内部版本:19041.450

【问题讨论】:

  • 4294967295-1 的无符号解释,这是控制台应用程序用来表示出现问题的常用值。看起来您的 Go 代码承诺传递配置文件,但实际上从未传递配置文件。 “出了点问题”是预期的答案。

标签: windows go exec


【解决方案1】:

问题是由于某种原因,即使在 shell 设置中,该命令也会失败。所以 我所做的是,除非输出为空,否则忽略错误:

package main

import (
   "fmt"
   "log"
   "os/exec"
)

func main() {
   o := exec.Command(
      "config-lint", "-terraform", "config-lint-master/example-files/config",
   )
   y, e := o.Output()
   if len(y) == 0 {
      log.Fatal(e)
   }
   fmt.Printf("%s", y)
}

或者你可以直接输出到控制台:

package main

import (
   "os"
   "os/exec"
)

func main() {
   o := exec.Command(
      "config-lint", "-terraform", "config-lint-master/example-files/config",
   )
   o.Stdout = os.Stdout
   o.Run()
}

【讨论】:

    猜你喜欢
    • 2021-12-04
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多