簡體   English   中英

exec.Command().Run() 未在 Windows 中正確執行命令

[英]exec.Command().Run() not executing command properly in Windows

我有一個名為 config-lint 的 linting 實用程序: https : //github.com/stelligent/config-lint ,我為 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 <FILE_OR_DIRECTORY_OF_TF_FILES>

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

Golang 版本:go 版本 go1.14.6 windows/amd64

操作系統:Win 10 Home,版本:19041.450

問題是,由於某種原因,即使在 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()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM