utils

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

utils package is a collection of useful modules needed to develop Redump

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckDir

func CheckDir(dir string) bool

CheckDir is a function to check if the specified directory exists. It returns true if it exists, or false if it does not.

Example
package main

import (
	"fmt"
	"github.com/tubone24/redump/pkg/utils"
)

func main() {
	resp := utils.CheckDir("test")
	fmt.Printf("%t", resp)
}
Output:

func DeepCopy

func DeepCopy(dst interface{}, src interface{}) error

DeepCopy is a function that completely copies (without passing by reference) even the elements contained in a structure.

Example
package main

import (
	"github.com/tubone24/redump/pkg/redmine"
	"github.com/tubone24/redump/pkg/utils"
)

func main() {
	source := redmine.Issue{
		Id:      1,
		Project: redmine.Project{Id: 1, Name: "testProject"},
		Tracker: redmine.Tracker{Id: 1, Name: "doing"},
		Status:  redmine.Status{Id: 1, Name: "test"}, Priority: redmine.Priority{Id: 1, Name: "High"},
		Author:      redmine.Author{Id: 1, Name: "testUser"},
		AssignedTo:  redmine.AssignedTo{Id: 1, Name: "testUser"},
		Subject:     "test1",
		Description: "testtesttesttest",
		StartDate:   "2020-01-01T00:00:00Z",
		CustomFields: redmine.CustomFields{&redmine.CustomField{
			Id:       1,
			Name:     "customField1",
			Multiple: true,
			Value:    []string{"aaaa", "bbb", "ccc"}}},
		CreatedOn: "2020-01-01T00:00:00Z",
		UpdatedOn: "2020-01-01T00:00:00Z",
		Attachments: redmine.Attachments{&redmine.Attachment{
			Id: 1, FileName: "test.png",
			FileSize:    12000,
			ContentUrl:  "http://example.com/test.png",
			Description: "testFile",
			Author:      redmine.Author{Id: 1, Name: "testUser"},
			CreatedOn:   "2020-01-01T00:00:00Z"}},
		Journals: redmine.Journals{&redmine.Journal{
			Id:        1,
			User:      redmine.User{Id: 1, Name: "testUser"},
			Notes:     "testtest",
			CreatedOn: "2020-01-01T00:00:00Z"},
			&redmine.Journal{
				Id:        2,
				User:      redmine.User{Id: 1, Name: "testUser"},
				Notes:     "testtest2",
				CreatedOn: "2020-01-01T00:00:00Z"},
			&redmine.Journal{
				Id:        3,
				User:      redmine.User{Id: 1, Name: "testUser"},
				Notes:     "testtest",
				CreatedOn: "2020-01-01T00:00:00Z", Details: redmine.Details{&redmine.Detail{
					Property: "change",
					Name:     "upload",
					OldValue: "aaa",
					NewValue: "bbb"}}},
		},
		Watchers: redmine.Watchers{&redmine.Watcher{
			Id: 1, Name: "testUser"}, &redmine.Watcher{Id: 2, Name: "testUser2"}, &redmine.Watcher{Id: 3, Name: "testUser3"}},
	}
	var destination redmine.Issue
	// Pass a pointer to a structure with the same type
	_ = utils.DeepCopy(&destination, &source)
}
Output:

func GetContentType

func GetContentType(key string) string

GetContentType returns the MIME type corresponding to the extension from the file name. For unregistered extensions, it returns "application/x-www-form-urlencoded" which does not indicate a specific MIME type.

Example
package main

import (
	"fmt"
	"github.com/tubone24/redump/pkg/utils"
)

func main() {
	resp := utils.GetContentType("test.png")
	// image/png
	fmt.Println(resp)
}
Output:

func MakeDir

func MakeDir(dir string) error

MakeDir recursively creates the specified directory. If the same directory exists, it will return without doing anything. If the same file exists, an error will occur.

Example
package main

import (
	"fmt"
	"github.com/tubone24/redump/pkg/utils"
)

func main() {
	_ = utils.MakeDir("test")
	resp := utils.CheckDir("test")
	// true
	fmt.Printf("%t", resp)
}
Output:

func NewProxyClient

func NewProxyClient(proxyUrl string) (*http.Client, error)

NewProxyClient can create an http client with proxy settings by specifying the URL of the HTTP Proxy.

Example
package main

import (
	"fmt"
	"github.com/tubone24/redump/pkg/utils"
)

func main() {
	proxy, _ := utils.NewProxyClient("http://tubone24:password@127.0.0.1:8080")
	client := utils.NewHttpClient(10000, utils.OptionHTTPClient(proxy))
	// via Proxy
	resp, _ := client.Get("https://aws-health-dashboard.vercel.app/api/aws")
	fmt.Println(string(resp))
}
Output:

func ReadFile

func ReadFile(file string) ([]byte, error)

ReadFile is a function that safely reads an object with a given file name using a buffer and returns a byte slice.

Example
package main

import (
	"fmt"
	"github.com/tubone24/redump/pkg/utils"
)

func main() {
	resp, _ := utils.ReadFile("test.json")
	fmt.Println(string(resp))
}
Output:

func SanitizeInvalidFileName

func SanitizeInvalidFileName(str string) (result string)

SanitizeInvalidFileName is a function to extract illegal characters from a file name. In the future, we plan to replace it with a URL encoding function.

Example
package main

import (
	"fmt"
	"github.com/tubone24/redump/pkg/utils"
)

func main() {
	resp := utils.SanitizeInvalidFileName("test test testtest test  test")
	// testtesttesttesttesttest
	fmt.Println(resp)
}
Output:

func WriteFile

func WriteFile(filename string, output []byte) error

WriteFile is a function that writes a file from a given byte slice.

Example
package main

import (
	"github.com/tubone24/redump/pkg/utils"
)

func main() {
	_ = utils.WriteFile("test.json", []byte("{\"some_json\": \"tubone24\"}"))
}
Output:

Types

type Api

type Api struct {
	// contains filtered or unexported fields
}

func NewHttpClient

func NewHttpClient(timeout int, opts ...Option) *Api

NewHttpClient is a function to create an HTTP client. When calling any of the methods, it is recommended to use the methods of the API structure generated by this function.

func (*Api) Delete

func (api *Api) Delete(url string) error

Delete function is a function to HTTP DELETE. The result can be obtained as a byte slice.

Example
package main

import (
	"github.com/tubone24/redump/pkg/utils"
)

func main() {
	client := utils.NewHttpClient(10000)
	_ = client.Delete("https://example.com/test")
}
Output:

func (*Api) Get

func (api *Api) Get(url string) ([]byte, error)

Get function is a function to HTTP GET. The result can be obtained as a byte slice.

Example
package main

import (
	"fmt"
	"github.com/tubone24/redump/pkg/utils"
)

func main() {
	client := utils.NewHttpClient(10000)
	resp, _ := client.Get("https://aws-health-dashboard.vercel.app/api/aws")
	fmt.Println(string(resp))
}
Output:

func (*Api) Post

func (api *Api) Post(url, contentType string, data []byte) ([]byte, error)

Post function is a function to HTTP POST. The result can be obtained as a byte slice.

Example
package main

import (
	"fmt"
	"github.com/tubone24/redump/pkg/utils"
)

func main() {
	client := utils.NewHttpClient(10000)
	resp, _ := client.Post("https://aws-health-dashboard.vercel.app/api/aws", "application/json", []byte("{\"test\": \"test\"}"))
	fmt.Println(string(resp))
}
Output:

func (*Api) Put

func (api *Api) Put(url, contentType string, data []byte) error

// Put function is a function to HTTP PUT. The result can be obtained as a byte slice.

Example
package main

import (
	"github.com/tubone24/redump/pkg/utils"
)

func main() {
	client := utils.NewHttpClient(10000)
	_ = client.Put("https://example.com/test", "application/json", []byte("{\"test\": \"test\"}"))
}
Output:

type HttpClientError

type HttpClientError struct {
	StatusCode int
}

func (*HttpClientError) Error

func (e *HttpClientError) Error() string

type Option

type Option func(*Api)

func OptionHTTPClient

func OptionHTTPClient(c *http.Client) Option

OptionHTTPClient is a function used to specify a custom client.

Jump to

Keyboard shortcuts

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