fileutils

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2023 License: MIT Imports: 8 Imported by: 1

README

fileutils

Go (golang) library with helper functions to manipulate files.

Installation

As a library

go get github.com/rgobbo/fileutils

Usage

In your Go app you can do something like

package main

import (
    "github.com/rgobbo/fileutils"
    "log"
    "os"
)

type Config struct {
    	HTTP          string         `json:"http.port"`
    	HTTPS         string         `json:"https.port"`
    	Host          string         `json:"host"`
    	TemplateDir   string         `json:"template.dir"`
    	StaticDir     string         `json:"http.static"`
    	HTTPError     string         `json:"http.error"`
    	DBType        string         `json:"db.type"`
    	DBServer      string         `json:"db.server"`
    	DBName        string         `json:"db.name"`
    	DBuser        string         `json:"db.user"`
    	DBPass        string         `json:"db.pass"`
}

func main() {
  confPath := "./config.json"
  var conf Config

  err := fileutils.LoadJson(confPath, &conf)
  if err != nil {
  	log.Fatal("Error loading json :", err)
  }

  log.Println("File processed successfully !!"


}

Or you can use a map[string] interface{} to load json files:

    var conf map[string]interface{}

      err := fileutils.LoadJson(confPath, &conf)
      if err != nil {
      	log.Fatal("Error loading json :", err)
      }

Documentation

LoadByteFiles(dirname string, ext string) ([]byte, error)
  • Read files from folder and return bytes, filtered by extension
LoadListFiles(path string, ext string, removeExtension bool) ([]string, error)
  • Loads a directory recursivily and returns a list path : directory path to start create the list ext : extension to filter, only files with a especific extension will be included into list , other files will bo ignored removeExtension : if true remove extension form the file name Example : LoadListFiles("/Users/test", ".html", true)
LoadJson(path string, obj interface{}) error
  • Load a json file and return into a inrterface Example : LoadJson("./file.json", &obj)
SaveJson(path string, obj interface{}) error
  • Convert a interface into json and save a file
LoadYaml(path string, obj interface{}) error
  • Load yaml file into a interface{} Example : LoadYaml("./file.yaml", &obj)
SaveYaml(path string, obj interface{}) error
  • Save yaml interface{} into a file
GetCWD() string
  • Return working dir
RenameIfExists(path string)
  • Rename a file if exists
GetTimeStamp() string
  • Return timeStamp string with current date

Working with compressed files

Compress

fileutils/compressor/zip
fileutils/compressor/targz
fileutils/compressor/tar compressor

Extract

fileutils/extractor/targz
fileutils/extractor/tar
fileutils/extractor/zip

Documentation

Index

Constants

View Source
const TIME_LAYOUT = "Jan-02-2006_15-04-05-MST"

Variables

This section is empty.

Functions

func CopyDir

func CopyDir(source string, dest string) (err error)

func CopyFile

func CopyFile(source string, dest string) (err error)

func GetCWD

func GetCWD() string

GetCWD - return working dir

func GetTimeStamp

func GetTimeStamp() string

GetTimeStamp - return timeStamp string with current date

func LoadByteFiles

func LoadByteFiles(dirname string, ext string) ([]byte, error)

LoadByteFiles - read files from folder and return bytes, filtered by extension

func LoadBytesDir

func LoadBytesDir(dirname string) ([]byte, error)

LoadBytesDir - read files from folder

func LoadFilesInfo

func LoadFilesInfo(path string) ([]map[string]interface{}, error)

LoadFilesInfo - loads a directory recursivily and returns a map[string]interface - list of infos path : directory path to start create the list Example : LoadFilesInfo("/Users/test")

func LoadJson

func LoadJson(path string, obj interface{}) error

LoadJson - Load a json file and return into a inrterface Example : LoadJson("./file.json", &obj)

func LoadListFiles

func LoadListFiles(path string, ext string, removeExtension bool) ([]string, error)

LoadListFiles - loads a directory recursivily and returns a list path : directory path to start create the list ext : extension to filter, only files with a especific extension will be included into list , other files will bo ignored removeExtension : if true remove extension form the file name Example : LoadListFiles("/Users/test", ".html", true)

func LoadYaml

func LoadYaml(path string, obj interface{}) error

LoadYaml - Load yaml file into a interface{} Example : LoadYaml("./file.yaml", &obj)

func RemoveDuplicates

func RemoveDuplicates(elements []string) []string

RemoveDuplicates - remove duplicate strings from slice string

func RenameIfExists

func RenameIfExists(path string)

RenameIfExists - rename a file if exists

func SaveJson

func SaveJson(path string, obj interface{}) error

SaveJson - Convert a interface into json and save a file

func SaveYaml

func SaveYaml(path string, obj interface{}) error

SaveYaml - save yaml interface{} into a file

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL