From 297136e0e23aae05ca48aa80b0a03e0d15899986 Mon Sep 17 00:00:00 2001 From: Hugo Thunnissen Date: Thu, 2 Jun 2022 18:13:15 +0200 Subject: [PATCH] Only create group inside container when it doesn't exist yet and add wkhtmltopdf --- Dockerfile | 2 +- main.go | 35 +++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 98acdbf..9c49735 100644 --- a/Dockerfile +++ b/Dockerfile @@ -127,7 +127,7 @@ RUN ln -s /opt/sonar-scanner-4.7.0.2747-linux/bin/sonar-scanner /usr/bin/sonar-s # Protobuf RUN apt-get -y update && DEBIAN_FRONTEND=noninteractive apt-get -y install protobuf-compiler -RUN apt-get -y update && DEBIAN_FRONTEND=noninteractive apt-get -y install html2text +RUN apt-get -y update && DEBIAN_FRONTEND=noninteractive apt-get -y install html2text wkhtmltopdf ADD . /opt/workspace-repo WORKDIR /opt/workspace-repo diff --git a/main.go b/main.go index 44161af..93ead8a 100644 --- a/main.go +++ b/main.go @@ -76,23 +76,30 @@ func AddUser(username string, uid string) error { } func AddGroup(name string, gid string) error { - cmd := exec.Command( - "groupadd", - "--gid", gid, - name, - ) - - output, err := cmd.CombinedOutput() + _, err := user.LookupGroupId(gid) if err != nil { - return fmt.Errorf( - "Error adding group: %w. Process error: %w. Process output: %s", - ErrAddGroupFailExit, - err, - output, - ) + if _, ok := err.(*user.UnknownGroupError); ok { + cmd := exec.Command( + "groupadd", + "--gid", gid, + name, + ) + + output, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf( + "Error adding group: %w. Process error: %w. Process output: %s", + ErrAddGroupFailExit, + err, + output, + ) + } + + return nil + } } - return nil + return err } const osDarwin = "darwin"