github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/cmd/fns/initialization/base/mod.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     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  
    18  package base
    19  
    20  import (
    21  	"context"
    22  	"github.com/aacfactory/errors"
    23  	"github.com/aacfactory/fns/cmd/generates/sources"
    24  	"golang.org/x/mod/modfile"
    25  	"os"
    26  	"path/filepath"
    27  )
    28  
    29  func NewModFile(goVersion string, path string, dir string) (f *ModFile, err error) {
    30  	if !filepath.IsAbs(dir) {
    31  		dir, err = filepath.Abs(dir)
    32  		if err != nil {
    33  			err = errors.Warning("fns: new mod file failed").WithCause(err).WithMeta("dir", dir)
    34  			return
    35  		}
    36  	}
    37  	f = &ModFile{
    38  		path:      path,
    39  		filename:  filepath.ToSlash(filepath.Join(dir, "go.mod")),
    40  		goVersion: goVersion,
    41  	}
    42  	return
    43  }
    44  
    45  type ModFile struct {
    46  	path      string
    47  	filename  string
    48  	goVersion string
    49  }
    50  
    51  func (f *ModFile) Name() (name string) {
    52  	name = f.filename
    53  	return
    54  }
    55  
    56  func (f *ModFile) Write(ctx context.Context) (err error) {
    57  	mf := &modfile.File{}
    58  	pathErr := mf.AddModuleStmt(f.path)
    59  	if pathErr != nil {
    60  		err = errors.Warning("fns: mod file write failed").WithCause(pathErr).WithMeta("filename", f.filename)
    61  		return
    62  	}
    63  	versionErr := mf.AddGoStmt(f.goVersion)
    64  	if versionErr != nil {
    65  		err = errors.Warning("fns: mod file write failed").WithCause(versionErr).WithMeta("filename", f.filename)
    66  		return
    67  	}
    68  	requires := []string{"github.com/aacfactory/fns"}
    69  	for _, require := range requires {
    70  		requireVersion, requireVersionErr := sources.LatestVersion(require)
    71  		if requireVersionErr != nil {
    72  			err = errors.Warning("fns: mod file write failed").WithCause(requireVersionErr).WithMeta("filename", f.filename)
    73  			return
    74  		}
    75  		fnsRequireErr := mf.AddRequire(require, requireVersion)
    76  		if fnsRequireErr != nil {
    77  			err = errors.Warning("fns: mod file write failed").WithCause(fnsRequireErr).WithMeta("filename", f.filename)
    78  			return
    79  		}
    80  	}
    81  	p, encodeErr := mf.Format()
    82  	if encodeErr != nil {
    83  		err = errors.Warning("fns: mod file write failed").WithCause(encodeErr).WithMeta("filename", f.filename)
    84  		return
    85  	}
    86  	writeErr := os.WriteFile(f.filename, p, 0644)
    87  	if writeErr != nil {
    88  		err = errors.Warning("fns: mod file write failed").WithCause(writeErr).WithMeta("filename", f.filename)
    89  		return
    90  	}
    91  	return
    92  }