Last active
October 6, 2020 21:51
-
-
Save jmrodri/4710d1fbd2641ab99bfa99cb654f404d to your computer and use it in GitHub Desktop.
SampleContext using Opts pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2020 The Operator-SDK Authors | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
package pkg | |
import ( | |
"fmt" | |
"path/filepath" | |
"strings" | |
"github.com/operator-framework/operator-sdk/test/utils" | |
) | |
// SampleContext represents the Context used to generate the samples | |
type SampleContext struct { | |
utils.TestContext | |
} | |
type SampleContextOpts func(s *SampleContext) error | |
func NewSampleContext(binary string, opts ...SampleContextOpts) (SampleContext, error) { | |
s := SampleContext{} | |
for _, o := range opts { | |
if err := o(&s); err != nil { | |
return s, err | |
} | |
} | |
return s, nil | |
} | |
func WithEnvironment(binary string, env ...string) SampleContextOpts { | |
return func(s *SampleContext) error { | |
var err error | |
s.TestContext, err = utils.NewTestContext(binary, env...) | |
return err | |
} | |
} | |
func WithPath(path string) SampleContextOpts { | |
return func(s *SampleContext) error { | |
if strings.TrimSpace(path) != "" { | |
path, err := filepath.Abs(path) | |
if err != nil { | |
return err | |
} | |
s.CmdContext.Dir = path | |
s.ProjectName = strings.ToLower(filepath.Base(s.Dir)) | |
s.ImageName = fmt.Sprintf("quay.io/example/%s:v0.0.1", s.ProjectName) | |
s.BundleImageName = fmt.Sprintf("quay.io/example/%s-bundle:v0.0.1", s.ProjectName) | |
} | |
} | |
} | |
func WithTestContext(tc *utils.TestContext) SampleContextOpts { | |
return func(s *SampleContext) error { | |
s.TestContext = *tc | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment