github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/runtime/source/go/golang.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/runtime/local/source/go/golang.go
    14  
    15  // Package golang is a source for Go
    16  package golang
    17  
    18  import (
    19  	"os"
    20  	"os/exec"
    21  	"path/filepath"
    22  	"strings"
    23  
    24  	"github.com/tickoalcantara12/micro/v3/service/runtime/source"
    25  )
    26  
    27  type Source struct {
    28  	Options source.Options
    29  	// Go Command
    30  	Cmd  string
    31  	Path string
    32  }
    33  
    34  func (g *Source) Fetch(url string) (*source.Repository, error) {
    35  	purl := url
    36  
    37  	if parts := strings.Split(url, "://"); len(parts) > 1 {
    38  		purl = parts[len(parts)-1]
    39  	}
    40  
    41  	// name of repo
    42  	name := filepath.Base(url)
    43  	// local path of repo
    44  	path := filepath.Join(g.Path, purl)
    45  	args := []string{"get", "-d", url, path}
    46  
    47  	cmd := exec.Command(g.Cmd, args...)
    48  	if err := cmd.Run(); err != nil {
    49  		return nil, err
    50  	}
    51  	return &source.Repository{
    52  		Name: name,
    53  		Path: path,
    54  		URL:  url,
    55  	}, nil
    56  }
    57  
    58  // Commit is not yet supported
    59  func (g *Source) Commit(r *source.Repository) error {
    60  	return nil
    61  }
    62  
    63  func (g *Source) String() string {
    64  	return "golang"
    65  }
    66  
    67  // whichGo locates the go command
    68  func whichGo() string {
    69  	// check GOROOT
    70  	if gr := os.Getenv("GOROOT"); len(gr) > 0 {
    71  		return filepath.Join(gr, "bin", "go")
    72  	}
    73  
    74  	// check path
    75  	for _, p := range filepath.SplitList(os.Getenv("PATH")) {
    76  		bin := filepath.Join(p, "go")
    77  		if _, err := os.Stat(bin); err == nil {
    78  			return bin
    79  		}
    80  	}
    81  
    82  	// best effort
    83  	return "go"
    84  }
    85  
    86  func NewSource(opts ...source.Option) source.Source {
    87  	options := source.Options{
    88  		Path: os.TempDir(),
    89  	}
    90  	for _, o := range opts {
    91  		o(&options)
    92  	}
    93  
    94  	cmd := whichGo()
    95  	path := options.Path
    96  
    97  	// point of no return
    98  	if len(cmd) == 0 {
    99  		panic("Could not find Go executable")
   100  	}
   101  
   102  	return &Source{
   103  		Options: options,
   104  		Cmd:     cmd,
   105  		Path:    path,
   106  	}
   107  }