github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/cmd/fns/initialization/base/main.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  	"os"
    24  	"path/filepath"
    25  	"strings"
    26  )
    27  
    28  func NewMainFile(path string, dir string, useWork bool) (mf *MainFile, err error) {
    29  	if !filepath.IsAbs(dir) {
    30  		dir, err = filepath.Abs(dir)
    31  		if err != nil {
    32  			err = errors.Warning("fns: new main file failed").WithCause(err).WithMeta("dir", dir)
    33  			return
    34  		}
    35  	}
    36  	mf = &MainFile{
    37  		path:     path,
    38  		filename: filepath.ToSlash(filepath.Join(dir, "main.go")),
    39  		useWork:  useWork,
    40  	}
    41  	return
    42  }
    43  
    44  type MainFile struct {
    45  	path     string
    46  	filename string
    47  	useWork  bool
    48  }
    49  
    50  func (f *MainFile) Name() (name string) {
    51  	name = f.filename
    52  	return
    53  }
    54  
    55  func (f *MainFile) Write(_ context.Context) (err error) {
    56  	const (
    57  		content = `package main
    58  
    59  import (
    60  	"github.com/aacfactory/fns"
    61  	"github.com/aacfactory/fns/context"
    62  	"#path#/modules"
    63  )
    64  
    65  var (
    66  	// Version
    67  	// go build -ldflags "-s -w -X main.Version=${VERSION}" -o fapp
    68  	Version = "v0.0.1"
    69  )
    70  
    71  //go:generate go run#mod_arg# #path#/internal/generator -v .
    72  func main() {
    73  	// set system environment to make config be active, e.g.: export FNS-ACTIVE=local
    74  	fns.
    75  		New(
    76  			fns.Version(Version),
    77  		).
    78  		Deploy(modules.Services()...).
    79  		Run(context.TODO()).
    80  		Sync()
    81  	return
    82  }
    83  `
    84  	)
    85  	modArg := ""
    86  	if !f.useWork {
    87  		modArg = " -mod=mod"
    88  	}
    89  	p := strings.ReplaceAll(content, "#mod_arg#", modArg)
    90  	p = strings.ReplaceAll(p, "#path#", f.path)
    91  	writeErr := os.WriteFile(f.filename, []byte(p), 0644)
    92  	if writeErr != nil {
    93  		err = errors.Warning("fns: main file write failed").WithCause(writeErr).WithMeta("filename", f.filename)
    94  		return
    95  	}
    96  	return
    97  }