github.com/kubevela/workflow@v0.6.0/version/version.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package version
    18  
    19  import "github.com/hashicorp/go-version"
    20  
    21  // GitRevision is the commit of repo
    22  var GitRevision = "UNKNOWN"
    23  
    24  // VelaVersion is the version of cli.
    25  var VelaVersion = "UNKNOWN"
    26  
    27  // IsOfficialWorkflowVersion checks whether the provided version string follows a KubeVela Workflow version pattern
    28  func IsOfficialWorkflowVersion(versionStr string) bool {
    29  	_, err := version.NewSemver(versionStr)
    30  	return err == nil
    31  }
    32  
    33  // GetOfficialWorkflowVersion extracts the KubeVela Workflow version from the provided string
    34  // More precisely, this method returns the segments and prerelease info w/o metadata
    35  func GetOfficialWorkflowVersion(versionStr string) (string, error) {
    36  	s, err := version.NewSemver(versionStr)
    37  	if err != nil {
    38  		return "", err
    39  	}
    40  	v := s.String()
    41  	metadata := s.Metadata()
    42  	if metadata != "" {
    43  		metadata = "+" + metadata
    44  	}
    45  	return v[:len(v)-len(metadata)], nil
    46  }