github.com/openimsdk/tools@v0.0.49/version/version.go (about)

     1  // Copyright © 2023 OpenIM. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package version
    16  
    17  import (
    18  	"fmt"
    19  	"runtime"
    20  
    21  	"github.com/openimsdk/tools/errs"
    22  	"gopkg.in/src-d/go-git.v4"
    23  )
    24  
    25  // Get returns the overall codebase version. It's for detecting
    26  // what code a binary was built from.
    27  func Get() Info {
    28  	// These variables typically come from -ldflags settings and in
    29  	// their absence fallback to the settings in ./base.go
    30  	return Info{
    31  		Major:        gitMajor,
    32  		Minor:        gitMinor,
    33  		GitVersion:   gitVersion,
    34  		GitTreeState: gitTreeState,
    35  		GitCommit:    gitCommit,
    36  		BuildDate:    buildDate,
    37  		GoVersion:    runtime.Version(),
    38  		Compiler:     runtime.Compiler,
    39  		Platform:     fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
    40  	}
    41  }
    42  
    43  // GetClientVersion returns the git version of the OpenIM client repository given a repository URL.
    44  func GetClientVersion(repoURL string) (*OpenIMClientVersion, error) {
    45  	clientVersion, err := getClientVersion(repoURL)
    46  	if err != nil {
    47  		return nil, errs.WrapMsg(err, "failed to get client version", "repoURL", repoURL)
    48  	}
    49  	return &OpenIMClientVersion{
    50  		ClientVersion: clientVersion,
    51  	}, nil
    52  }
    53  
    54  func getClientVersion(repoURL string) (string, error) {
    55  	// Temp directory for cloning could be made more unique or cleaned up after use
    56  	tempDir := "/tmp/openim-sdk-core"
    57  
    58  	// Consider checking if the repo already exists and just fetch updates instead of cloning every time
    59  	repo, err := git.PlainClone(tempDir, false, &git.CloneOptions{
    60  		URL: repoURL,
    61  	})
    62  	if err != nil {
    63  		return "", errs.WrapMsg(err, "failed to clone OpenIM client repository", "repoURL", repoURL)
    64  	}
    65  
    66  	ref, err := repo.Head()
    67  	if err != nil {
    68  		return "", errs.WrapMsg(err, "failed to get head reference", "repoURL", repoURL)
    69  	}
    70  
    71  	return ref.Hash().String(), nil
    72  }
    73  
    74  // GetSingleVersion returns single version of sealer.
    75  func GetSingleVersion() string {
    76  	return gitVersion
    77  }