Skip to content

Commit

Permalink
Merge pull request #97 from dreddsa5dies/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
dreddsa5dies authored Feb 4, 2024
2 parents 702ff65 + 257ba93 commit 85f5738
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
53 changes: 35 additions & 18 deletions projects/26_getFileInfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,48 @@ import (
"fmt"
"log"
"os"
)

var (
fileInfo os.FileInfo
err error
"path/filepath"
)

func main() {
if len(os.Args) == 1 {
fmt.Fprintf(os.Stdout, "Using: %s file\n", os.Args[0])
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s [-r] <file_or_directory>\n", os.Args[0])
os.Exit(1)
}

fileInfo, err = os.Stat(os.Args[1])
if err != nil {
if os.IsNotExist(err) {
log.Fatal("File does not exist.")
path := os.Args[len(os.Args)-1]
recursive := false
if len(os.Args) == 3 && os.Args[1] == "-r" {
recursive = true
}

if recursive {
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return nil
}
printOut(info)
return nil
})
if err != nil {
log.Fatal(err)
}
} else {
fileInfo, err := os.Stat(path)
if err != nil {
log.Fatal(err)
}
printOut(fileInfo)
}
}

fmt.Println("File name:", fileInfo.Name())
fmt.Println("Size in bytes:", fileInfo.Size())
fmt.Println("Permissions:", fileInfo.Mode())
fmt.Println("Last modified:", fileInfo.ModTime())
fmt.Println("Is Directory: ", fileInfo.IsDir())
fmt.Printf("System interface type: %T\n", fileInfo.Sys())
fmt.Printf("System info: %+v\n\n", fileInfo.Sys())
func printOut(info os.FileInfo) {
fmt.Println("File name:", info.Name())
fmt.Println("Size in bytes:", info.Size())
fmt.Println("Permissions:", info.Mode())
fmt.Println("Last modified:", info.ModTime())
fmt.Println("Is Directory: ", info.IsDir())
fmt.Printf("System interface type: %T\n", info.Sys())
fmt.Printf("System info: %+v\n\n", info.Sys())
}
17 changes: 12 additions & 5 deletions projects/52_findAltSites/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
package main

import (
"context"
"crypto/md5" //nolint:gosec // it's need
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"time"
)

func main() {
Expand Down Expand Up @@ -37,11 +39,16 @@ func main() {
}

// save requests from user-agents
resultReq := make(map[string]string)
resultReq := make(map[string]string, len(userAgents))

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// iterate
for k, v := range userAgents {
resultReq[k] = doReq(inputURL, v)
ctxReq, cancelReq := context.WithDeadline(ctx, time.Now().Add(5*time.Second))
resultReq[k] = doReqCtx(ctxReq, inputURL, v)
cancelReq()
}

// The next part of the code creates an md5s array and then iterates through the responses,
Expand Down Expand Up @@ -69,11 +76,11 @@ func main() {
os.Exit(0)
}

// doReq - get body request
func doReq(inputURL, userAgent string) string {
// doReqCtx - get body request
func doReqCtx(ctx context.Context, inputURL, userAgent string) string {
client := &http.Client{}

req, err := http.NewRequest("GET", inputURL, http.NoBody)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, inputURL, http.NoBody)
if err != nil {
log.Fatalln(err)
}
Expand Down

0 comments on commit 85f5738

Please sign in to comment.