github.com/aacfactory/fns@v1.2.85/cmd/fns/initialization/base/base.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  	"fmt"
    23  	"github.com/aacfactory/errors"
    24  	"github.com/aacfactory/fns/cmd/generates/files"
    25  	"github.com/aacfactory/fns/cmd/generates/processes"
    26  	"path/filepath"
    27  	"time"
    28  )
    29  
    30  func Write(ctx context.Context, goVersion string, path string, dockerImageName string, work bool, dir string) (err error) {
    31  	if files.ExistFile(filepath.Join(dir, "go.mod")) {
    32  		err = errors.Warning("fnc: go.mod is exist")
    33  		return
    34  	}
    35  	process := processes.New()
    36  	// mod
    37  	mod, modErr := NewModFile(goVersion, path, dir)
    38  	if modErr != nil {
    39  		err = modErr
    40  		return
    41  	}
    42  	process.Add("mod: writing", Unit(mod))
    43  	// configs
    44  	configs, configsErr := NewConfigFiles(dir)
    45  	if configsErr != nil {
    46  		err = configsErr
    47  		return
    48  	}
    49  	configsUints := make([]processes.Unit, 0, 1)
    50  	for _, config := range configs {
    51  		configsUints = append(configsUints, Unit(config))
    52  	}
    53  	process.Add("configs: writing", configsUints...)
    54  	// hooks
    55  	hooks, hooksErr := NewHooksFile(dir)
    56  	if hooksErr != nil {
    57  		err = hooksErr
    58  		return
    59  	}
    60  	process.Add("hooks: writing", Unit(hooks))
    61  	// internal >>>
    62  	generator, generatorErr := NewInternalGeneratorsFile(dir)
    63  	if generatorErr != nil {
    64  		err = generatorErr
    65  		return
    66  	}
    67  	process.Add("generator: writing", Unit(generator))
    68  	// internal <<<
    69  	// modules
    70  	modules, modulesErr := NewModulesFile(path, dir)
    71  	if modulesErr != nil {
    72  		err = modulesErr
    73  		return
    74  	}
    75  	process.Add("modules: writing", Unit(modules))
    76  	// repositories
    77  	repositories, repositoriesErr := NewRepositoryFile(dir)
    78  	if repositoriesErr != nil {
    79  		err = repositoriesErr
    80  		return
    81  	}
    82  	process.Add("repositories: writing", Unit(repositories))
    83  	// main
    84  	main, mainErr := NewMainFile(path, dir, work)
    85  	if mainErr != nil {
    86  		err = mainErr
    87  		return
    88  	}
    89  	process.Add("main: writing", Unit(main))
    90  	// docker file
    91  	if dockerImageName == "" {
    92  		dockerImageName = DockerImageNameFromMod(path)
    93  	}
    94  	docker, dockerErr := NewDockerFile(goVersion, path, dir, dockerImageName)
    95  	if dockerErr != nil {
    96  		err = dockerErr
    97  		return
    98  	}
    99  	process.Add("Dockerfile: writing", Unit(docker))
   100  	// work
   101  	if work {
   102  		wk, wkErr := NewWorkFile(goVersion, path, dir)
   103  		if wkErr != nil {
   104  			err = wkErr
   105  			return
   106  		}
   107  		process.Add("work: writing", Unit(wk))
   108  	}
   109  
   110  	// start
   111  	results := process.Start(ctx)
   112  	for {
   113  		result, ok := <-results
   114  		if !ok {
   115  			break
   116  		}
   117  		fmt.Println(result)
   118  		if result.Error != nil {
   119  			err = result.Error
   120  			_ = process.Abort(1 * time.Second)
   121  			return
   122  		}
   123  	}
   124  	return
   125  }