Fix path in LogFile creator
Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
This commit is contained in:
		
							
								
								
									
										16
									
								
								cio/io.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								cio/io.go
									
									
									
									
									
								
							| @@ -258,10 +258,11 @@ func BinaryIO(binary string, args map[string]string) Creator { | |||||||
| 			q.Set(k, v) | 			q.Set(k, v) | ||||||
| 		} | 		} | ||||||
| 		uri.RawQuery = q.Encode() | 		uri.RawQuery = q.Encode() | ||||||
|  | 		res := uri.String() | ||||||
| 		return &logURI{ | 		return &logURI{ | ||||||
| 			config: Config{ | 			config: Config{ | ||||||
| 				Stdout: uri.String(), | 				Stdout: res, | ||||||
| 				Stderr: uri.String(), | 				Stderr: res, | ||||||
| 			}, | 			}, | ||||||
| 		}, nil | 		}, nil | ||||||
| 	} | 	} | ||||||
| @@ -271,14 +272,19 @@ func BinaryIO(binary string, args map[string]string) Creator { | |||||||
| // If the log file already exists, the logs will be appended to the file. | // If the log file already exists, the logs will be appended to the file. | ||||||
| func LogFile(path string) Creator { | func LogFile(path string) Creator { | ||||||
| 	return func(_ string) (IO, error) { | 	return func(_ string) (IO, error) { | ||||||
|  | 		path = filepath.Clean(path) | ||||||
|  | 		if !strings.HasPrefix(path, "/") { | ||||||
|  | 			return nil, errors.New("absolute path needed") | ||||||
|  | 		} | ||||||
| 		uri := &url.URL{ | 		uri := &url.URL{ | ||||||
| 			Scheme: "file", | 			Scheme: "file", | ||||||
| 			Host:   path, | 			Path:   path, | ||||||
| 		} | 		} | ||||||
|  | 		res := uri.String() | ||||||
| 		return &logURI{ | 		return &logURI{ | ||||||
| 			config: Config{ | 			config: Config{ | ||||||
| 				Stdout: uri.String(), | 				Stdout: res, | ||||||
| 				Stderr: uri.String(), | 				Stderr: res, | ||||||
| 			}, | 			}, | ||||||
| 		}, nil | 		}, nil | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -177,3 +177,21 @@ func TestBinaryIOFailOnRelativePath(t *testing.T) { | |||||||
| 	_, err := BinaryIO("./bin", nil)("!") | 	_, err := BinaryIO("./bin", nil)("!") | ||||||
| 	assert.Error(t, err, "absolute path needed") | 	assert.Error(t, err, "absolute path needed") | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestLogFileAbsolutePath(t *testing.T) { | ||||||
|  | 	res, err := LogFile("/full/path/file.txt")("!") | ||||||
|  | 	assert.NilError(t, err) | ||||||
|  | 	assert.Equal(t, "file:///full/path/file.txt", res.Config().Stdout) | ||||||
|  | 	assert.Equal(t, "file:///full/path/file.txt", res.Config().Stderr) | ||||||
|  |  | ||||||
|  | 	// Test parse back | ||||||
|  | 	parsed, err := url.Parse(res.Config().Stdout) | ||||||
|  | 	assert.NilError(t, err) | ||||||
|  | 	assert.Equal(t, "file", parsed.Scheme) | ||||||
|  | 	assert.Equal(t, "/full/path/file.txt", parsed.Path) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestLogFileFailOnRelativePath(t *testing.T) { | ||||||
|  | 	_, err := LogFile("./file.txt")("!") | ||||||
|  | 	assert.Error(t, err, "absolute path needed") | ||||||
|  | } | ||||||
|   | |||||||
| @@ -103,11 +103,11 @@ func createIO(ctx context.Context, id string, ioUID, ioGID int, stdio proc.Stdio | |||||||
| 	case "binary": | 	case "binary": | ||||||
| 		pio.io, err = NewBinaryIO(ctx, id, u) | 		pio.io, err = NewBinaryIO(ctx, id, u) | ||||||
| 	case "file": | 	case "file": | ||||||
| 		if err := os.MkdirAll(filepath.Dir(u.Host), 0755); err != nil { | 		if err := os.MkdirAll(filepath.Dir(u.Path), 0755); err != nil { | ||||||
| 			return nil, err | 			return nil, err | ||||||
| 		} | 		} | ||||||
| 		var f *os.File | 		var f *os.File | ||||||
| 		f, err = os.OpenFile(u.Host, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | 		f, err = os.OpenFile(u.Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return nil, err | 			return nil, err | ||||||
| 		} | 		} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Maksym Pavlenko
					Maksym Pavlenko