github.com/wawandco/oxplugins@v0.7.11/tools/flect/initializer.go (about)

     1  package flect
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  )
     8  
     9  type Initializer struct{}
    10  
    11  func (i Initializer) Name() string {
    12  	return "flect/initializer"
    13  }
    14  
    15  func (i *Initializer) Initialize(ctx context.Context, root string, args []string) error {
    16  
    17  	rootYml := root + "/inflections.yml"
    18  
    19  	content := `
    20  	{
    21  	  "singular": "plural"
    22  	}
    23  	`
    24  
    25  	_, err := os.Stat(rootYml)
    26  	if err == nil {
    27  
    28  		fmt.Println("inflections.yml file already exist ")
    29  		return nil
    30  
    31  	}
    32  	if os.IsNotExist(err) {
    33  
    34  		// create file if it does not exist
    35  		file, err := os.Create(rootYml)
    36  
    37  		if err != nil {
    38  			return (err)
    39  		}
    40  
    41  		_, err = os.OpenFile(rootYml, os.O_RDWR, 0644)
    42  		if err != nil {
    43  			return (err)
    44  		}
    45  
    46  		_, err = file.WriteString(content)
    47  		if err != nil {
    48  			return (err)
    49  		}
    50  
    51  		file.Close()
    52  
    53  		return nil
    54  
    55  	}
    56  
    57  	return err
    58  
    59  }