code.cestus.io/tools/fabricator@v0.4.3/pkg/helpers/go.go (about)

     1  package helpers
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"code.cestus.io/tools/fabricator/pkg/fabricator"
    12  )
    13  
    14  // GuessGoImportPath guesses to Go import path for the specified folder.
    15  func GuessGoImportPath(ctx context.Context, io fabricator.IOStreams, root string) (goImportPath string, err error) {
    16  	defer func() {
    17  		if err != nil {
    18  			err = fmt.Errorf("guessing Go import path at `%s`: %s", root, err)
    19  		}
    20  	}()
    21  
    22  	executor := NewExecutor(root, io).WithEnv("GOWORK", "off")
    23  	// If a Go import path is defined, use it.
    24  	if goImportPath, err = executor.Output(ctx, "go", "list", "-f", "{{ .ImportPath }}"); err == nil {
    25  		return strings.TrimSpace(goImportPath), nil
    26  	}
    27  
    28  	mod, err := GetGoModule(ctx, io, root)
    29  
    30  	if err != nil {
    31  		return "", err
    32  	}
    33  
    34  	return mod.GetRelativeImportPath(root)
    35  }
    36  
    37  type GoModule struct {
    38  	Path string `json:"Path"`
    39  	Dir  string `json:"Dir"`
    40  }
    41  
    42  func (m GoModule) String() string {
    43  	return m.Path
    44  }
    45  
    46  func (m GoModule) GetRelativeImportPath(dir string) (_ string, err error) {
    47  	defer func() {
    48  		if err != nil {
    49  			err = fmt.Errorf("getting relative import path for `%s`: %s", dir, err)
    50  		}
    51  	}()
    52  
    53  	relPath, err := filepath.Rel(m.Dir, dir)
    54  
    55  	if err != nil {
    56  		return "", fmt.Errorf("unable to determine relative path: %s", err)
    57  	}
    58  
    59  	return path.Join(m.Path, filepath.ToSlash(relPath)), nil
    60  }
    61  
    62  func GetGoModule(ctx context.Context, io fabricator.IOStreams, root string) (_ *GoModule, err error) {
    63  	defer func() {
    64  		if err != nil {
    65  			err = fmt.Errorf("getting Go module at `%s`: %s", root, err)
    66  		}
    67  	}()
    68  
    69  	executor := NewExecutor(root, io).WithEnv("GOWORK", "off")
    70  
    71  	var result GoModule
    72  	err = executor.JSONOutput(ctx, &result, "go", "list", "-m", "-json")
    73  
    74  	return &result, nil
    75  }
    76  
    77  func GetGoPackage(ctx context.Context, io fabricator.IOStreams, pkg string) (_ *GoModule, err error) {
    78  	defer func() {
    79  		if err != nil {
    80  			err = fmt.Errorf("getting Go package for `%s`: %s", pkg, err)
    81  		}
    82  	}()
    83  
    84  	wd, err := os.Getwd()
    85  
    86  	if err != nil {
    87  		return nil, fmt.Errorf("could not determine working directory")
    88  	}
    89  
    90  	executor := NewExecutor(wd, io).WithEnv("GOWORK", "off")
    91  	var result GoModule
    92  	err = executor.JSONOutput(ctx, &result, "go", "list", "-json", pkg)
    93  
    94  	return &result, nil
    95  }
    96  
    97  func GetGoPackageNameFromGoImportPath(goImportPath string) string {
    98  	return strings.Replace(path.Base(goImportPath), "-", "", -1)
    99  }