Skip to content

Commit

Permalink
fix: unable to build multigroup projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Camila Macedo committed May 9, 2020
1 parent 45ade82 commit 3e00f2b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
39 changes: 38 additions & 1 deletion pkg/scaffold/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ limitations under the License.
package scaffold

import (
"fmt"
"io/ioutil"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model/config"
)

Expand All @@ -38,5 +42,38 @@ func NewEditScaffolder(config *config.Config, multigroup bool) Scaffolder {
// Scaffold implements Scaffolder
func (s *editScaffolder) Scaffold() error {
s.config.MultiGroup = s.multigroup
return nil
filename := "Dockerfile"
bs, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
str := string(bs)

// update dockerfile
if s.multigroup {
str, err = ensureExistAndReplace(
str,
"COPY api/ api/",
`COPY apis/ apis/`)
if err != nil {
return err
}
} else {
str, err = ensureExistAndReplace(
str,
"COPY apis/ apis/",
`COPY api/ api/`)
if err != nil {
return err
}
}

return ioutil.WriteFile(filename, []byte(str), 0644)
}

func ensureExistAndReplace(input, match, replace string) (string, error) {
if !strings.Contains(input, match) {
return "", fmt.Errorf("can't find %q", match)
}
return strings.Replace(input, match, replace, -1), nil
}
2 changes: 1 addition & 1 deletion testdata/project-v2-multigroup/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RUN go mod download

# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY apis/ apis/
COPY controllers/ controllers/

# Build
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v3-multigroup/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RUN go mod download

# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY apis/ apis/
COPY controllers/ controllers/

# Build
Expand Down

0 comments on commit 3e00f2b

Please sign in to comment.